|
15 | 15 | select these parameters:
|
16 | 16 | http://docs.astropy.org/en/stable/visualization/histogram.html
|
17 | 17 | """
|
18 |
| - |
| 18 | +# %% |
19 | 19 | import matplotlib.pyplot as plt
|
20 | 20 | import numpy as np
|
21 | 21 |
|
|
27 | 27 | fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2)
|
28 | 28 |
|
29 | 29 | colors = ['red', 'tan', 'lime']
|
30 |
| -ax0.hist(x, n_bins, density=True, histtype='bar', color=colors, |
31 |
| - label=colors, hatch=['o', '*', '.'], |
32 |
| - edgecolor=['black', 'red', 'blue'], linewidth=[1, 2, 3]) |
| 30 | +ax0.hist(x, n_bins, density=True, histtype="bar", color=colors, label=colors) |
33 | 31 | ax0.legend(prop={'size': 10})
|
34 | 32 | ax0.set_title('bars with legend')
|
35 | 33 |
|
36 |
| -ax1.hist( |
37 |
| - x, n_bins, density=True, histtype="bar", stacked=True, |
38 |
| - edgecolor=["black", "yellow", "blue"]) |
| 34 | +ax1.hist(x, n_bins, density=True, histtype="bar", stacked=True) |
39 | 35 | ax1.set_title('stacked bar')
|
40 | 36 |
|
41 |
| -ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False, |
42 |
| - linewidth=[1, 2, 3]) |
| 37 | +ax2.hist(x, n_bins, histtype="step", stacked=True, fill=False) |
43 | 38 | ax2.set_title('stack step (unfilled)')
|
44 | 39 |
|
45 | 40 | # Make a multiple-histogram of data-sets with different length.
|
46 | 41 | x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
|
47 |
| -ax3.hist(x_multi, n_bins, histtype='bar', hatch=['\\', '/', '-']) |
| 42 | +ax3.hist(x_multi, n_bins, histtype="bar") |
48 | 43 | ax3.set_title('different sample sizes')
|
49 | 44 |
|
50 | 45 | fig.tight_layout()
|
51 | 46 | plt.show()
|
52 | 47 |
|
| 48 | +# %% |
| 49 | +# Setting properties for each sample set |
| 50 | +# ------------ |
| 51 | +# |
| 52 | +# Plotting a bar chart with sample sets differentiated using: |
| 53 | +# |
| 54 | +# * edgecolors |
| 55 | +# * hatches |
| 56 | +# * linewidths |
| 57 | +# * linestyles |
| 58 | +# |
| 59 | +# Also, these parameters can be specified for all types of |
| 60 | +# histograms (stacked, step, etc.) and not just for the *bar* |
| 61 | +# type histogram as shown in the example. |
| 62 | + |
| 63 | +hatches = ["-", "o", "x"] |
| 64 | +linewidths = [1, 2, 3] |
| 65 | +edgecolors = ["green", "red", "blue"] |
| 66 | +linestyles = ["-", ":", "--"] |
| 67 | + |
| 68 | +fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2) |
| 69 | + |
| 70 | +ax0.hist( |
| 71 | + x, n_bins, density=True, fill=False, histtype="bar", |
| 72 | + edgecolor=edgecolors, label=edgecolors |
| 73 | +) |
| 74 | +ax0.legend(prop={"size": 10}) |
| 75 | +ax0.set_title("Bars with Edgecolors") |
| 76 | + |
| 77 | +ax1.hist( |
| 78 | + x, n_bins, density=True, histtype="bar", |
| 79 | + hatch=hatches, label=hatches |
| 80 | +) |
| 81 | +ax1.legend(prop={"size": 10}) |
| 82 | +ax1.set_title("Bars with Hatches") |
| 83 | + |
| 84 | +ax2.hist( |
| 85 | + x, n_bins, density=True, fill=False, histtype="bar", |
| 86 | + linewidth=linewidths, edgecolor=edgecolors, label=linewidths |
| 87 | +) |
| 88 | +ax2.legend(prop={"size": 10}) |
| 89 | +ax2.set_title("Bars with Linewidths") |
| 90 | + |
| 91 | +ax3.hist( |
| 92 | + x, n_bins, density=True, fill=False, histtype="bar", |
| 93 | + linestyle=linestyles, edgecolor=edgecolors, label=linestyles |
| 94 | +) |
| 95 | +ax3.legend(prop={"size": 10}) |
| 96 | +ax3.set_title("Bars with Linestyles") |
| 97 | + |
| 98 | +fig.tight_layout() |
| 99 | +plt.show() |
| 100 | + |
53 | 101 | # %%
|
54 | 102 | #
|
55 | 103 | # .. admonition:: References
|
|
0 commit comments