diff --git a/doc/users/prev_whats_new/whats_new_1.5.rst b/doc/users/prev_whats_new/whats_new_1.5.rst index dd8e204aa957..039f65e2eba6 100644 --- a/doc/users/prev_whats_new/whats_new_1.5.rst +++ b/doc/users/prev_whats_new/whats_new_1.5.rst @@ -368,11 +368,6 @@ kwargs names is not ideal, but `.Axes.fill_between` already has a This is particularly useful for plotting pre-binned histograms. -.. figure:: ../../gallery/lines_bars_and_markers/images/sphx_glr_filled_step_001.png - :target: ../../gallery/lines_bars_and_markers/filled_step.html - :align: center - :scale: 50 - Square Plot ``````````` diff --git a/galleries/examples/lines_bars_and_markers/filled_step.py b/galleries/examples/lines_bars_and_markers/filled_step.py deleted file mode 100644 index 65a7d31a425a..000000000000 --- a/galleries/examples/lines_bars_and_markers/filled_step.py +++ /dev/null @@ -1,237 +0,0 @@ -""" -========================= -Hatch-filled histograms -========================= - -Hatching capabilities for plotting histograms. -""" - -from functools import partial -import itertools - -from cycler import cycler - -import matplotlib.pyplot as plt -import numpy as np - -import matplotlib.ticker as mticker - - -def filled_hist(ax, edges, values, bottoms=None, orientation='v', - **kwargs): - """ - Draw a histogram as a stepped patch. - - Parameters - ---------- - ax : Axes - The Axes to plot to. - - edges : array - A length n+1 array giving the left edges of each bin and the - right edge of the last bin. - - values : array - A length n array of bin counts or values - - bottoms : float or array, optional - A length n array of the bottom of the bars. If None, zero is used. - - orientation : {'v', 'h'} - Orientation of the histogram. 'v' (default) has - the bars increasing in the positive y-direction. - - **kwargs - Extra keyword arguments are passed through to `.fill_between`. - - Returns - ------- - ret : PolyCollection - Artist added to the Axes - """ - print(orientation) - if orientation not in 'hv': - raise ValueError(f"orientation must be in {{'h', 'v'}} " - f"not {orientation}") - - kwargs.setdefault('step', 'post') - kwargs.setdefault('alpha', 0.7) - edges = np.asarray(edges) - values = np.asarray(values) - if len(edges) - 1 != len(values): - raise ValueError(f'Must provide one more bin edge than value not: ' - f'{len(edges)=} {len(values)=}') - - if bottoms is None: - bottoms = 0 - bottoms = np.broadcast_to(bottoms, values.shape) - - values = np.append(values, values[-1]) - bottoms = np.append(bottoms, bottoms[-1]) - if orientation == 'h': - return ax.fill_betweenx(edges, values, bottoms, - **kwargs) - elif orientation == 'v': - return ax.fill_between(edges, values, bottoms, - **kwargs) - else: - raise AssertionError("you should never be here") - - -def stack_hist(ax, stacked_data, sty_cycle, bottoms=None, - hist_func=None, labels=None, - plot_func=None, plot_kwargs=None): - """ - Parameters - ---------- - ax : axes.Axes - The Axes to add artists to. - - stacked_data : array or Mapping - A (M, N) shaped array. The first dimension will be iterated over to - compute histograms row-wise - - sty_cycle : Cycler or operable of dict - Style to apply to each set - - bottoms : array, default: 0 - The initial positions of the bottoms. - - hist_func : callable, optional - Must have signature `bin_vals, bin_edges = f(data)`. - `bin_edges` expected to be one longer than `bin_vals` - - labels : list of str, optional - The label for each set. - - If not given and stacked data is an array defaults to 'default set {n}' - - If *stacked_data* is a mapping, and *labels* is None, default to the - keys. - - If *stacked_data* is a mapping and *labels* is given then only the - columns listed will be plotted. - - plot_func : callable, optional - Function to call to draw the histogram must have signature: - - ret = plot_func(ax, edges, top, bottoms=bottoms, - label=label, **kwargs) - - plot_kwargs : dict, optional - Any extra keyword arguments to pass through to the plotting function. - This will be the same for all calls to the plotting function and will - override the values in *sty_cycle*. - - Returns - ------- - arts : dict - Dictionary of artists keyed on their labels - """ - # deal with default binning function - if hist_func is None: - hist_func = np.histogram - - # deal with default plotting function - if plot_func is None: - plot_func = filled_hist - - # deal with default - if plot_kwargs is None: - plot_kwargs = {} - print(plot_kwargs) - try: - l_keys = stacked_data.keys() - label_data = True - if labels is None: - labels = l_keys - - except AttributeError: - label_data = False - if labels is None: - labels = itertools.repeat(None) - - if label_data: - loop_iter = enumerate((stacked_data[lab], lab, s) - for lab, s in zip(labels, sty_cycle)) - else: - loop_iter = enumerate(zip(stacked_data, labels, sty_cycle)) - - arts = {} - for j, (data, label, sty) in loop_iter: - if label is None: - label = f'dflt set {j}' - label = sty.pop('label', label) - vals, edges = hist_func(data) - if bottoms is None: - bottoms = np.zeros_like(vals) - top = bottoms + vals - print(sty) - sty.update(plot_kwargs) - print(sty) - ret = plot_func(ax, edges, top, bottoms=bottoms, - label=label, **sty) - bottoms = top - arts[label] = ret - ax.legend(fontsize=10) - return arts - - -# set up histogram function to fixed bins -edges = np.linspace(-3, 3, 20, endpoint=True) -hist_func = partial(np.histogram, bins=edges) - -# set up style cycles -color_cycle = cycler(facecolor=plt.rcParams['axes.prop_cycle'][:4]) -label_cycle = cycler(label=[f'set {n}' for n in range(4)]) -hatch_cycle = cycler(hatch=['/', '*', '+', '|']) - -# Fixing random state for reproducibility -np.random.seed(19680801) - -stack_data = np.random.randn(4, 12250) -dict_data = dict(zip((c['label'] for c in label_cycle), stack_data)) - -# %% -# Work with plain arrays - -fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4.5), tight_layout=True) -arts = stack_hist(ax1, stack_data, color_cycle + label_cycle + hatch_cycle, - hist_func=hist_func) - -arts = stack_hist(ax2, stack_data, color_cycle, - hist_func=hist_func, - plot_kwargs=dict(edgecolor='w', orientation='h')) -ax1.set_ylabel('counts') -ax1.set_xlabel('x') -ax2.set_xlabel('counts') -ax2.set_ylabel('x') - -# %% -# Work with labeled data - -fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4.5), - tight_layout=True, sharey=True) - -arts = stack_hist(ax1, dict_data, color_cycle + hatch_cycle, - hist_func=hist_func) - -arts = stack_hist(ax2, dict_data, color_cycle + hatch_cycle, - hist_func=hist_func, labels=['set 0', 'set 3']) -ax1.xaxis.set_major_locator(mticker.MaxNLocator(5)) -ax1.set_xlabel('counts') -ax1.set_ylabel('x') -ax2.set_ylabel('x') - -plt.show() - -# %% -# -# .. admonition:: References -# -# The use of the following functions, methods, classes and modules is shown -# in this example: -# -# - `matplotlib.axes.Axes.fill_betweenx` / `matplotlib.pyplot.fill_betweenx` -# - `matplotlib.axes.Axes.fill_between` / `matplotlib.pyplot.fill_between` -# - `matplotlib.axis.Axis.set_major_locator` diff --git a/galleries/examples/statistics/barchart_demo.py b/galleries/examples/statistics/barchart_demo.py deleted file mode 100644 index ad33f442b844..000000000000 --- a/galleries/examples/statistics/barchart_demo.py +++ /dev/null @@ -1,111 +0,0 @@ -""" -=================================== -Percentiles as horizontal bar chart -=================================== - -Bar charts are useful for visualizing counts, or summary statistics -with error bars. Also see the :doc:`/gallery/lines_bars_and_markers/barchart` -or the :doc:`/gallery/lines_bars_and_markers/barh` example for simpler versions -of those features. - -This example comes from an application in which grade school gym -teachers wanted to be able to show parents how their child did across -a handful of fitness tests, and importantly, relative to how other -children did. To extract the plotting code for demo purposes, we'll -just make up some data for little Johnny Doe. -""" - -from collections import namedtuple - -import matplotlib.pyplot as plt -import numpy as np - -Student = namedtuple('Student', ['name', 'grade', 'gender']) -Score = namedtuple('Score', ['value', 'unit', 'percentile']) - - -def to_ordinal(num): - """Convert an integer to an ordinal string, e.g. 2 -> '2nd'.""" - suffixes = {str(i): v - for i, v in enumerate(['th', 'st', 'nd', 'rd', 'th', - 'th', 'th', 'th', 'th', 'th'])} - v = str(num) - # special case early teens - if v in {'11', '12', '13'}: - return v + 'th' - return v + suffixes[v[-1]] - - -def format_score(score): - """ - Create score labels for the right y-axis as the test name followed by the - measurement unit (if any), split over two lines. - """ - return f'{score.value}\n{score.unit}' if score.unit else str(score.value) - - -def plot_student_results(student, scores_by_test, cohort_size): - fig, ax1 = plt.subplots(figsize=(9, 7), layout='constrained') - fig.canvas.manager.set_window_title('Eldorado K-8 Fitness Chart') - - ax1.set_title(student.name) - ax1.set_xlabel( - 'Percentile Ranking Across {grade} Grade {gender}s\n' - 'Cohort Size: {cohort_size}'.format( - grade=to_ordinal(student.grade), - gender=student.gender.title(), - cohort_size=cohort_size)) - - test_names = list(scores_by_test.keys()) - percentiles = [score.percentile for score in scores_by_test.values()] - - rects = ax1.barh(test_names, percentiles, align='center', height=0.5) - # Partition the percentile values to be able to draw large numbers in - # white within the bar, and small numbers in black outside the bar. - large_percentiles = [to_ordinal(p) if p > 40 else '' for p in percentiles] - small_percentiles = [to_ordinal(p) if p <= 40 else '' for p in percentiles] - ax1.bar_label(rects, small_percentiles, - padding=5, color='black', fontweight='bold') - ax1.bar_label(rects, large_percentiles, - padding=-32, color='white', fontweight='bold') - - ax1.set_xlim([0, 100]) - ax1.set_xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) - ax1.xaxis.grid(True, linestyle='--', which='major', - color='grey', alpha=.25) - ax1.axvline(50, color='grey', alpha=0.25) # median position - - # Set the right-hand Y-axis ticks and labels - ax2 = ax1.twinx() - # Set equal limits on both yaxis so that the ticks line up - ax2.set_ylim(ax1.get_ylim()) - # Set the tick locations and labels - ax2.set_yticks( - np.arange(len(scores_by_test)), - labels=[format_score(score) for score in scores_by_test.values()]) - - ax2.set_ylabel('Test Scores') - - -student = Student(name='Johnny Doe', grade=2, gender='Boy') -scores_by_test = { - 'Pacer Test': Score(7, 'laps', percentile=37), - 'Flexed Arm\n Hang': Score(48, 'sec', percentile=95), - 'Mile Run': Score('12:52', 'min:sec', percentile=73), - 'Agility': Score(17, 'sec', percentile=60), - 'Push Ups': Score(14, '', percentile=16), -} - -plot_student_results(student, scores_by_test, cohort_size=62) -plt.show() - -# %% -# -# .. admonition:: References -# -# The use of the following functions, methods, classes and modules is shown -# in this example: -# -# - `matplotlib.axes.Axes.bar` / `matplotlib.pyplot.bar` -# - `matplotlib.axes.Axes.bar_label` / `matplotlib.pyplot.bar_label` -# - `matplotlib.axes.Axes.twinx` / `matplotlib.pyplot.twinx` diff --git a/galleries/examples/statistics/histogram_multihist.py b/galleries/examples/statistics/histogram_multihist.py index b9a9c5f0bf26..62b3c4178ced 100644 --- a/galleries/examples/statistics/histogram_multihist.py +++ b/galleries/examples/statistics/histogram_multihist.py @@ -14,6 +14,9 @@ shape of a histogram. The Astropy docs have a great section on how to select these parameters: http://docs.astropy.org/en/stable/visualization/histogram.html + +.. redirect-from:: /gallery/lines_bars_and_markers/filled_step + """ # %% import matplotlib.pyplot as plt