箱形图抽屉功能

此示例演示如何将预先计算的箱形图统计信息传递到框图抽屉。第一个图演示了如何删除和添加单个组件(请注意,平均值是默认情况下未显示的唯一值)。第二个图展示了如何定制艺术风格。

关于箱形图及其历史的一个很好的一般参考可以在这里找到:http://vita.had.co.nz/papers/boxplots.pdf

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.cbook as cbook
  4. # fake data
  5. np.random.seed(19680801)
  6. data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
  7. labels = list('ABCD')
  8. # compute the boxplot stats
  9. stats = cbook.boxplot_stats(data, labels=labels, bootstrap=10000)

在我们计算了统计数据之后,我们可以通过并改变任何事情。 为了证明这一点,我将每组的中位数设置为所有数据的中位数,并将均值加倍

  1. for n in range(len(stats)):
  2. stats[n]['med'] = np.median(data)
  3. stats[n]['mean'] *= 2
  4. print(list(stats[0]))
  5. fs = 10 # fontsize

输出:

  1. ['label', 'mean', 'iqr', 'cilo', 'cihi', 'whishi', 'whislo', 'fliers', 'q1', 'med', 'q3']

演示如何切换不同元素的显示:

  1. fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6, 6), sharey=True)
  2. axes[0, 0].bxp(stats)
  3. axes[0, 0].set_title('Default', fontsize=fs)
  4. axes[0, 1].bxp(stats, showmeans=True)
  5. axes[0, 1].set_title('showmeans=True', fontsize=fs)
  6. axes[0, 2].bxp(stats, showmeans=True, meanline=True)
  7. axes[0, 2].set_title('showmeans=True,\nmeanline=True', fontsize=fs)
  8. axes[1, 0].bxp(stats, showbox=False, showcaps=False)
  9. tufte_title = 'Tufte Style\n(showbox=False,\nshowcaps=False)'
  10. axes[1, 0].set_title(tufte_title, fontsize=fs)
  11. axes[1, 1].bxp(stats, shownotches=True)
  12. axes[1, 1].set_title('notch=True', fontsize=fs)
  13. axes[1, 2].bxp(stats, showfliers=False)
  14. axes[1, 2].set_title('showfliers=False', fontsize=fs)
  15. for ax in axes.flatten():
  16. ax.set_yscale('log')
  17. ax.set_yticklabels([])
  18. fig.subplots_adjust(hspace=0.4)
  19. plt.show()

箱形图抽屉功能示例

演示如何自定义显示不同的元素:

  1. boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')
  2. flierprops = dict(marker='o', markerfacecolor='green', markersize=12,
  3. linestyle='none')
  4. medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')
  5. meanpointprops = dict(marker='D', markeredgecolor='black',
  6. markerfacecolor='firebrick')
  7. meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')
  8. fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(6, 6), sharey=True)
  9. axes[0, 0].bxp(stats, boxprops=boxprops)
  10. axes[0, 0].set_title('Custom boxprops', fontsize=fs)
  11. axes[0, 1].bxp(stats, flierprops=flierprops, medianprops=medianprops)
  12. axes[0, 1].set_title('Custom medianprops\nand flierprops', fontsize=fs)
  13. axes[1, 0].bxp(stats, meanprops=meanpointprops, meanline=False,
  14. showmeans=True)
  15. axes[1, 0].set_title('Custom mean\nas point', fontsize=fs)
  16. axes[1, 1].bxp(stats, meanprops=meanlineprops, meanline=True,
  17. showmeans=True)
  18. axes[1, 1].set_title('Custom mean\nas line', fontsize=fs)
  19. for ax in axes.flatten():
  20. ax.set_yscale('log')
  21. ax.set_yticklabels([])
  22. fig.suptitle("I never said they'd be pretty")
  23. fig.subplots_adjust(hspace=0.4)
  24. plt.show()

箱形图抽屉功能

下载这个示例