============================================================= Demo of the histogram (hist) function with multiple data sets ============================================================= Plot histogram with multiple sample sets and demonstrate: * Use of legend with multiple
()
| 41 | return fig |
| 42 | |
| 43 | def PyplotHistogram(): |
| 44 | """ |
| 45 | ============================================================= |
| 46 | Demo of the histogram (hist) function with multiple data sets |
| 47 | ============================================================= |
| 48 | |
| 49 | Plot histogram with multiple sample sets and demonstrate: |
| 50 | |
| 51 | * Use of legend with multiple sample sets |
| 52 | * Stacked bars |
| 53 | * Step curve with no fill |
| 54 | * Data sets of different sample sizes |
| 55 | |
| 56 | Selecting different bin counts and sizes can significantly affect the |
| 57 | shape of a histogram. The Astropy docs have a great section on how to |
| 58 | select these parameters: |
| 59 | http://docs.astropy.org/en/stable/visualization/histogram.html |
| 60 | """ |
| 61 | |
| 62 | import numpy as np |
| 63 | import matplotlib.pyplot as plt |
| 64 | |
| 65 | np.random.seed(0) |
| 66 | |
| 67 | n_bins = 10 |
| 68 | x = np.random.randn(1000, 3) |
| 69 | |
| 70 | fig, axes = plt.subplots(nrows=2, ncols=2) |
| 71 | ax0, ax1, ax2, ax3 = axes.flatten() |
| 72 | |
| 73 | colors = ['red', 'tan', 'lime'] |
| 74 | ax0.hist(x, n_bins, normed=1, histtype='bar', color=colors, label=colors) |
| 75 | ax0.legend(prop={'size': 10}) |
| 76 | ax0.set_title('bars with legend') |
| 77 | |
| 78 | ax1.hist(x, n_bins, normed=1, histtype='bar', stacked=True) |
| 79 | ax1.set_title('stacked bar') |
| 80 | |
| 81 | ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False) |
| 82 | ax2.set_title('stack step (unfilled)') |
| 83 | |
| 84 | # Make a multiple-histogram of data-sets with different length. |
| 85 | x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]] |
| 86 | ax3.hist(x_multi, n_bins, histtype='bar') |
| 87 | ax3.set_title('different sample sizes') |
| 88 | |
| 89 | fig.tight_layout() |
| 90 | return fig |
| 91 | |
| 92 | def PyplotArtistBoxPlots(): |
| 93 | """ |