Boxplot 演示

boxplot 的代码示例。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Fixing random state for reproducibility
  4. np.random.seed(19680801)
  5. # fake up some data
  6. spread = np.random.rand(50) * 100
  7. center = np.ones(25) * 50
  8. flier_high = np.random.rand(10) * 100 + 100
  9. flier_low = np.random.rand(10) * -100
  10. data = np.concatenate((spread, center, flier_high, flier_low))
  1. fig1, ax1 = plt.subplots()
  2. ax1.set_title('Basic Plot')
  3. ax1.boxplot(data)

Boxplot示例

  1. fig2, ax2 = plt.subplots()
  2. ax2.set_title('Notched boxes')
  3. ax2.boxplot(data, notch=True)

Boxplot示例2

  1. green_diamond = dict(markerfacecolor='g', marker='D')
  2. fig3, ax3 = plt.subplots()
  3. ax3.set_title('Changed Outlier Symbols')
  4. ax3.boxplot(data, flierprops=green_diamond)

Boxplot示例3

  1. fig4, ax4 = plt.subplots()
  2. ax4.set_title('Hide Outlier Points')
  3. ax4.boxplot(data, showfliers=False)

Boxplot示例4

  1. red_square = dict(markerfacecolor='r', marker='s')
  2. fig5, ax5 = plt.subplots()
  3. ax5.set_title('Horizontal Boxes')
  4. ax5.boxplot(data, vert=False, flierprops=red_square)

Boxplot示例5

  1. fig6, ax6 = plt.subplots()
  2. ax6.set_title('Shorter Whisker Length')
  3. ax6.boxplot(data, flierprops=red_square, vert=False, whis=0.75)

Boxplot示例6

模拟一些更多的数据。

  1. spread = np.random.rand(50) * 100
  2. center = np.ones(25) * 40
  3. flier_high = np.random.rand(10) * 100 + 100
  4. flier_low = np.random.rand(10) * -100
  5. d2 = np.concatenate((spread, center, flier_high, flier_low))
  6. data.shape = (-1, 1)
  7. d2.shape = (-1, 1)

仅当所有列的长度相同时,才能生成二维数组。 如果不是,则使用列表。 这实际上更有效,因为boxplot无论如何都会在内部将2-D数组转换为向量列表。

  1. data = [data, d2, d2[::2,0]]
  2. fig7, ax7 = plt.subplots()
  3. ax7.set_title('Multiple Samples with Different sizes')
  4. ax7.boxplot(data)
  5. plt.show()

Boxplot示例7

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.boxplot
  3. matplotlib.pyplot.boxplot

下载这个示例