Skip to content

Commit 382b4bc

Browse files
authored
Merge pull request #1182 from MetOffice/use_json
Add fixes for vertical line xlim and PDF xlim
2 parents 1ae2b12 + 1e78def commit 382b4bc

File tree

1 file changed

+46
-57
lines changed

1 file changed

+46
-57
lines changed

src/CSET/operators/plot.py

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ def _plot_and_save_vertical_line_series(
614614
ax.ticklabel_format(axis="x")
615615
ax.tick_params(axis="y")
616616

617+
# Add gridlines
618+
ax.grid(linestyle="--", color="grey", linewidth=1)
619+
617620
# Save plot.
618621
fig.savefig(filename, bbox_inches="tight", dpi=_get_plot_resolution())
619622
logging.info("Saved line plot to %s", filename)
@@ -691,7 +694,6 @@ def _plot_and_save_histogram_series(
691694
title: str,
692695
vmin: float,
693696
vmax: float,
694-
histtype: str = "step",
695697
**kwargs,
696698
):
697699
"""Plot and save a histogram series.
@@ -700,15 +702,6 @@ def _plot_and_save_histogram_series(
700702
----------
701703
cubes: Cube or CubeList
702704
2 dimensional Cube or CubeList of the data to plot as histogram.
703-
Plotting options are fixed:
704-
density=True, histtype='step',stacked=True to ensure that
705-
a probability density is plotted using matplotlib.pyplot.hist
706-
to plot the probability density so that the area under
707-
the histogram integrates to 1.
708-
stacked is set to True so the sum of the histograms is
709-
normalized to 1.
710-
ax.autoscale is switched off and the ylim range
711-
is preset as (0,1) to make figures comparable.
712705
filename: str
713706
Filename of the plot to write.
714707
title: str
@@ -717,10 +710,6 @@ def _plot_and_save_histogram_series(
717710
minimum for colorbar
718711
vmax: float
719712
maximum for colorbar
720-
histtype: str
721-
The type of histogram to plot. Options are "step" for a line
722-
histogram or "barstacked", "stepfilled". "Step" is the default option,
723-
but can be changed in the rose-suite.conf configuration.
724713
"""
725714
fig = plt.figure(figsize=(10, 10), facecolor="w", edgecolor="k")
726715
ax = plt.gca()
@@ -774,7 +763,6 @@ def _plot_and_save_postage_stamp_histogram_series(
774763
stamp_coordinate: str,
775764
vmin: float,
776765
vmax: float,
777-
histtype: str,
778766
**kwargs,
779767
):
780768
"""Plot and save postage (ensemble members) stamps for a histogram series.
@@ -783,15 +771,6 @@ def _plot_and_save_postage_stamp_histogram_series(
783771
----------
784772
cube: Cube
785773
2 dimensional Cube of the data to plot as histogram.
786-
Plotting options are fixed:
787-
density=True, histtype='bar', stacked=True to ensure that
788-
a probability density is plotted using matplotlib.pyplot.hist
789-
to plot the probability density so that the area under
790-
the histogram integrates to 1.
791-
stacked is set to True so the sum of the histograms is
792-
normalized to 1.
793-
ax.autoscale is switched off and the ylim range
794-
is preset as (0,1) to make figures comparable.
795774
filename: str
796775
Filename of the plot to write.
797776
title: str
@@ -802,11 +781,6 @@ def _plot_and_save_postage_stamp_histogram_series(
802781
minimum for pdf x-axis
803782
vmax: float
804783
maximum for pdf x-axis
805-
histtype: str
806-
The type of histogram to plot. Options are "step" for a line
807-
histogram or "barstacked", "stepfilled". "Step" is the default option,
808-
but can be changed in the rose-suite.conf configuration.
809-
810784
"""
811785
# Use the smallest square grid that will fit the members.
812786
grid_size = int(math.ceil(math.sqrt(len(cube.coord(stamp_coordinate).points))))
@@ -822,7 +796,7 @@ def _plot_and_save_postage_stamp_histogram_series(
822796
# Reshape cube data into a single array to allow for a single histogram.
823797
# Otherwise we plot xdim histograms stacked.
824798
member_data_1d = (member.data).flatten()
825-
plt.hist(member_data_1d, density=True, histtype=histtype, stacked=True)
799+
plt.hist(member_data_1d, density=True, stacked=True)
826800
ax = plt.gca()
827801
ax.set_title(f"Member #{member.coord(stamp_coordinate).points[0]}")
828802
ax.set_xlim(vmin, vmax)
@@ -842,7 +816,6 @@ def _plot_and_save_postage_stamps_in_single_plot_histogram_series(
842816
stamp_coordinate: str,
843817
vmin: float,
844818
vmax: float,
845-
histtype: str = "step",
846819
**kwargs,
847820
):
848821
fig, ax = plt.subplots(figsize=(10, 10), facecolor="w", edgecolor="k")
@@ -858,7 +831,6 @@ def _plot_and_save_postage_stamps_in_single_plot_histogram_series(
858831
plt.hist(
859832
member_data_1d,
860833
density=True,
861-
histtype=histtype,
862834
stacked=True,
863835
label=f"Member #{member.coord(stamp_coordinate).points[0]}",
864836
)
@@ -1188,6 +1160,9 @@ def plot_vertical_line_series(
11881160
# Initialise empty list to hold all data from all cubes in a CubeList
11891161
all_data = []
11901162

1163+
# Store min/max ranges for x range.
1164+
x_levels = []
1165+
11911166
# Iterate over all cubes in cube or CubeList and plot.
11921167
for cube_iter in iter_maybe(cubes):
11931168
# Ensure we've got a single cube.
@@ -1209,18 +1184,26 @@ def plot_vertical_line_series(
12091184
f"Cube must have a {sequence_coordinate} coordinate or be 1D."
12101185
) from err
12111186

1212-
# Append cube data to the list to calculate vmin and vmax across entire
1213-
# cubelist.
1214-
all_data.append(cube_iter.data)
1187+
# Get minimum and maximum from levels information.
1188+
_, levels, _ = _colorbar_map_levels(cube_iter)
1189+
if levels is not None:
1190+
x_levels.append(min(levels))
1191+
x_levels.append(max(levels))
1192+
else:
1193+
all_data.append(cube_iter.data)
12151194

1216-
# Combine all data into a single NumPy array
1217-
combined_data = np.concatenate(all_data)
1195+
if len(x_levels) == 0:
1196+
# Combine all data into a single NumPy array
1197+
combined_data = np.concatenate(all_data)
12181198

1219-
# Set the lower and upper limit for the x-axis to ensure all plots have same
1220-
# range. This needs to read the whole cube over the range of the sequence
1221-
# and if applicable postage stamp coordinate.
1222-
vmin = np.floor(combined_data.min())
1223-
vmax = np.ceil(combined_data.max())
1199+
# Set the lower and upper limit for the x-axis to ensure all plots have same
1200+
# range. This needs to read the whole cube over the range of the sequence
1201+
# and if applicable postage stamp coordinate.
1202+
vmin = np.floor(combined_data.min())
1203+
vmax = np.ceil(combined_data.max())
1204+
else:
1205+
vmin = min(x_levels)
1206+
vmax = max(x_levels)
12241207

12251208
# Create a plot for each value of the sequence coordinate.
12261209
# Allowing for multiple cubes in a CubeList to be plotted in the same plot for
@@ -1356,7 +1339,6 @@ def plot_histogram_series(
13561339
sequence_coordinate: str = "time",
13571340
stamp_coordinate: str = "realization",
13581341
single_plot: bool = False,
1359-
histtype: str = "step",
13601342
**kwargs,
13611343
) -> iris.cube.Cube | iris.cube.CubeList:
13621344
"""Plot a histogram plot for each vertical level provided.
@@ -1389,10 +1371,6 @@ def plot_histogram_series(
13891371
If True, all postage stamp plots will be plotted in a single plot. If
13901372
False, each postage stamp plot will be plotted separately. Is only valid
13911373
if stamp_coordinate exists and has more than a single point.
1392-
histtype: str, optional
1393-
The type of histogram to plot. Options are "step" for a line histogram
1394-
or "barstacked", "stepfilled". "Step" is the default option, but can be
1395-
changed in the rose-suite.conf configuration.
13961374
13971375
Returns
13981376
-------
@@ -1419,6 +1397,9 @@ def plot_histogram_series(
14191397
# Initialise empty list to hold all data from all cubes in a CubeList
14201398
all_data = []
14211399

1400+
# Store min/max ranges for xlim.
1401+
x_levels = []
1402+
14221403
# Iterate over all cubes in cube or CubeList and plot.
14231404
for cube_iter in iter_maybe(cubes):
14241405
# Ensure we've got a single cube.
@@ -1447,17 +1428,26 @@ def plot_histogram_series(
14471428
f"Cube must have a {sequence_coordinate} coordinate."
14481429
) from err
14491430

1450-
# append cube data to the list to calculate vmin and vmax across entire cubelist
1451-
all_data.append(cube_iter.data)
1431+
# Get minimum and maximum from levels information.
1432+
_, levels, _ = _colorbar_map_levels(cube_iter)
1433+
if levels is not None:
1434+
x_levels.append(min(levels))
1435+
x_levels.append(max(levels))
1436+
else:
1437+
all_data.append(cube_iter.data)
14521438

1453-
# Combine all data into a single NumPy array
1454-
combined_data = np.concatenate(all_data)
1439+
if len(x_levels) == 0:
1440+
# Combine all data into a single NumPy array
1441+
combined_data = np.concatenate(all_data)
14551442

1456-
# Set the lower and upper limit for the colorbar to ensure all plots have
1457-
# same range. This needs to read the whole cube over the range of the
1458-
# sequence and if applicable postage stamp coordinate.
1459-
vmin = np.floor((combined_data.min()))
1460-
vmax = np.ceil((combined_data.max()))
1443+
# Set the lower and upper limit for the x-axis to ensure all plots have same
1444+
# range. This needs to read the whole cube over the range of the sequence
1445+
# and if applicable postage stamp coordinate.
1446+
vmin = np.floor(combined_data.min())
1447+
vmax = np.ceil(combined_data.max())
1448+
else:
1449+
vmin = min(x_levels)
1450+
vmax = max(x_levels)
14611451

14621452
# Create a plot for each value of the sequence coordinate.
14631453
# Allowing for multiple cubes in a CubeList to be plotted in the same plot for
@@ -1480,7 +1470,6 @@ def plot_histogram_series(
14801470
title=title,
14811471
vmin=vmin,
14821472
vmax=vmax,
1483-
histtype=histtype,
14841473
)
14851474
plot_index.append(plot_filename)
14861475

0 commit comments

Comments
 (0)