并排生成多个直方图

此示例沿范畴x轴绘制不同样本的水平直方图。此外,直方图被绘制成与它们的x位置对称,从而使它们与小提琴图非常相似。

为了使这个高度专门化的绘制,我们不能使用标准的 hist 方法。相反,我们使用 barh 直接绘制水平线。通过 np.histogram 函数计算棒材的垂直位置和长度。使用相同的范围(最小和最大值)和存储箱数量计算所有采样的直方图,以便每个采样的存储箱位于相同的垂直位置。

选择不同的存储量和大小会显著影响直方图的形状。Astropy文档有很多关于如何选择这些参数的部分: http://docs.astropy.org/en/stable/visualization/histogram.html

并排生成多个直方图示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. np.random.seed(19680801)
  4. number_of_bins = 20
  5. # An example of three data sets to compare
  6. number_of_data_points = 387
  7. labels = ["A", "B", "C"]
  8. data_sets = [np.random.normal(0, 1, number_of_data_points),
  9. np.random.normal(6, 1, number_of_data_points),
  10. np.random.normal(-3, 1, number_of_data_points)]
  11. # Computed quantities to aid plotting
  12. hist_range = (np.min(data_sets), np.max(data_sets))
  13. binned_data_sets = [
  14. np.histogram(d, range=hist_range, bins=number_of_bins)[0]
  15. for d in data_sets
  16. ]
  17. binned_maximums = np.max(binned_data_sets, axis=1)
  18. x_locations = np.arange(0, sum(binned_maximums), np.max(binned_maximums))
  19. # The bin_edges are the same for all of the histograms
  20. bin_edges = np.linspace(hist_range[0], hist_range[1], number_of_bins + 1)
  21. centers = 0.5 * (bin_edges + np.roll(bin_edges, 1))[:-1]
  22. heights = np.diff(bin_edges)
  23. # Cycle through and plot each histogram
  24. fig, ax = plt.subplots()
  25. for x_loc, binned_data in zip(x_locations, binned_data_sets):
  26. lefts = x_loc - 0.5 * binned_data
  27. ax.barh(centers, binned_data, height=heights, left=lefts)
  28. ax.set_xticks(x_locations)
  29. ax.set_xticklabels(labels)
  30. ax.set_ylabel("Data values")
  31. ax.set_xlabel("Data sets")
  32. plt.show()

下载这个示例