|
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, label=colors) |
| 30 | +ax0.hist(x, n_bins, density=True, histtype='bar', color=colors, label=colors) |
31 | 31 | ax0.legend(prop={'size': 10})
|
32 | 32 | ax0.set_title('bars with legend')
|
33 | 33 |
|
34 |
| -ax1.hist(x, n_bins, density=True, histtype="bar", stacked=True) |
| 34 | +ax1.hist(x, n_bins, density=True, histtype='bar', stacked=True) |
35 | 35 | ax1.set_title('stacked bar')
|
36 | 36 |
|
37 |
| -ax2.hist(x, n_bins, histtype="step", stacked=True, fill=False) |
| 37 | +ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False) |
38 | 38 | ax2.set_title('stack step (unfilled)')
|
39 | 39 |
|
40 | 40 | # Make a multiple-histogram of data-sets with different length.
|
41 | 41 | x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
|
42 |
| -ax3.hist(x_multi, n_bins, histtype="bar") |
| 42 | +ax3.hist(x_multi, n_bins, histtype='bar') |
43 | 43 | ax3.set_title('different sample sizes')
|
44 | 44 |
|
45 | 45 | fig.tight_layout()
|
46 | 46 | plt.show()
|
47 | 47 |
|
48 | 48 | # %%
|
49 |
| -# Setting properties for each sample set |
50 |
| -# -------------------------------------- |
| 49 | +# ----------------------------------- |
| 50 | +# Setting properties for each dataset |
| 51 | +# ----------------------------------- |
51 | 52 | #
|
52 |
| -# Plotting a bar chart with sample sets differentiated using: |
| 53 | +# Plotting bar charts with datasets differentiated using: |
53 | 54 | #
|
54 | 55 | # * edgecolors
|
55 | 56 | # * hatches
|
56 | 57 | # * linewidths
|
57 | 58 | # * linestyles
|
58 | 59 | #
|
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. |
| 60 | +# |
| 61 | +# Histograms with Edge-Colors |
| 62 | +# ........................... |
62 | 63 |
|
63 |
| -hatches = ["-", "o", "x"] |
64 |
| -linewidths = [1, 2, 3] |
65 |
| -edgecolors = ["green", "red", "blue"] |
66 |
| -linestyles = ["-", ":", "--"] |
| 64 | +fig, ax = plt.subplots() |
67 | 65 |
|
68 |
| -fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2) |
| 66 | +edgecolors = ['green', 'red', 'blue'] |
69 | 67 |
|
70 |
| -ax0.hist( |
71 |
| - x, n_bins, density=True, fill=False, histtype="bar", |
| 68 | +ax.hist( |
| 69 | + x, n_bins, fill=False, histtype="step", stacked=True, |
72 | 70 | edgecolor=edgecolors, label=edgecolors
|
73 | 71 | )
|
74 |
| -ax0.legend(prop={"size": 10}) |
75 |
| -ax0.set_title("Bars with Edgecolors") |
| 72 | +ax.legend() |
| 73 | +ax.set_title('Stacked Steps with Edgecolors') |
76 | 74 |
|
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") |
| 75 | +plt.show() |
| 76 | + |
| 77 | + |
| 78 | +# Histograms with Hatches |
| 79 | +# ....................... |
| 80 | + |
| 81 | +fig, ax = plt.subplots() |
| 82 | + |
| 83 | +hatches = [".", "o", "x"] |
| 84 | + |
| 85 | +ax.hist(x, n_bins, histtype="barstacked", hatch=hatches, label=hatches) |
| 86 | +ax.legend() |
| 87 | +ax.set_title("Hatches on Stacked Bars") |
| 88 | + |
| 89 | +plt.show() |
83 | 90 |
|
84 |
| -ax2.hist( |
85 |
| - x, n_bins, density=True, fill=False, histtype="bar", |
86 |
| - linewidth=linewidths, edgecolor=edgecolors, label=linewidths |
| 91 | + |
| 92 | +# Histograms with Linewidths |
| 93 | +# .......................... |
| 94 | + |
| 95 | +fig, ax = plt.subplots() |
| 96 | + |
| 97 | +linewidths = [1, 2, 3] |
| 98 | +edgecolors = ["green", "red", "blue"] |
| 99 | + |
| 100 | +ax.hist( |
| 101 | + x, n_bins, fill=False, histtype="bar", linewidth=linewidths, |
| 102 | + edgecolor=edgecolors, label=linewidths, |
87 | 103 | )
|
88 |
| -ax2.legend(prop={"size": 10}) |
89 |
| -ax2.set_title("Bars with Linewidths") |
| 104 | +ax.legend() |
| 105 | +ax.set_title("Bars with Linewidths") |
90 | 106 |
|
91 |
| -ax3.hist( |
92 |
| - x, n_bins, density=True, fill=False, histtype="bar", |
93 |
| - linestyle=linestyles, edgecolor=edgecolors, label=linestyles |
| 107 | +plt.show() |
| 108 | + |
| 109 | + |
| 110 | +# Histograms with LineStyles |
| 111 | +# ........................... |
| 112 | + |
| 113 | +fig, ax = plt.subplots() |
| 114 | + |
| 115 | +linestyles = ['-', ':', '--'] |
| 116 | + |
| 117 | +ax.hist( |
| 118 | + x, n_bins, fill=False, histtype='bar', linestyle=linestyles, |
| 119 | + edgecolor=edgecolors, label=linestyles |
94 | 120 | )
|
95 |
| -ax3.legend(prop={"size": 10}) |
96 |
| -ax3.set_title("Bars with Linestyles") |
| 121 | +ax.legend() |
| 122 | +ax.set_title('Bars with Linestyles') |
97 | 123 |
|
98 |
| -fig.tight_layout() |
99 | 124 | plt.show()
|
100 | 125 |
|
101 | 126 | # %%
|
|
0 commit comments