如何将具有不同列长度的数据表转换为python中的箱图

时间:2015-12-08 17:50:27

标签: python numpy matplotlib statistics

我正在尝试绘制一组数据。以下是一个示例

34456.11718 34767.75017 34392.43452 34356.77702 34552.49424
34618.99645 34845.01827 34470.82192 34755.43286 34490.34788
34524.72989 34838.92508 34464.9628  34685.11467 34583.76757
34525.16889 34837.37895 34541.02059 34555.63599 34527.23146
34531.52852 34770.05856 34568.93382 34504.1442  34514.08938
34431.45985 34585.67336 34527.43138 34071.39518 34533.3499
34329.05653 34536.24601 34606.97121 34132.64443 34439.45988
34268.17505 34611.81836 34608.9584  34075.29279 34474.64524
34477.37305 34699.84967 34646.87148 33802.50334 34441.68268
34674.54769 34632.93716 34672.11406 33906.01839 34457.02467
34576.99168 34722.83457 34548.2877  34040.83828 34531.64214
34534.72687 34633.32703 34627.64169 34370.23821 34520.28533
34598.89338 34598.50237 34717.49512 34415.73371 34426.95518
34462.53642 34549.27221 34663.26121 34114.69248 34375.88882
34385.24535 34549.24708 34713.14059 33865.1237  
34429.79659 34538.31632 34716.97048 34280.39958 
34652.65947 34523.53762 34698.31976 34492.3672  
34645.35515 34546.5272  34598.81003 34531.15127 
34630.22884 34591.32164 34597.6796  34362.6228  
34720.29415 34451.27535 34746.00718 34233.68801 
34645.84702             34636.53923 34031.19302 
34389.24407             34134.58745 34431.07515

实际的数组要长得多,但关键特性是它有5列,每列都在不同的文本文件中命名。这是我的代码:

import numpy as np

import matplotlib.pyplot as plt


plt.show()

tube1data = np.genfromtxt("speed1.txt")
tube2data = np.genfromtxt("speed2.txt")
tube3data = np.genfromtxt("speed3.txt")
tube4data = np.genfromtxt("speed4.txt")
tube5data = np.genfromtxt("speed5.txt")

plt.boxplot(tube1data,patch_artist=True)
plt.boxplot(tube2data,patch_artist=True)
plt.boxplot(tube3data,patch_artist=True)
plt.boxplot(tube4data,patch_artist=True)
plt.boxplot(tube5data,patch_artist=True)


plt.show()

The problems is it overlays all of them on one plot

1 个答案:

答案 0 :(得分:1)

boxplot将很乐意绘制一系列数据分布。它们不需要长度相同。

在你的情况下:

plt.boxplot([tube1data, tube2data, <...>], patch_artist=True)

作为一个更完整的例子:

import numpy as np
import matplotlib.pyplot as plt

lengths = [100, 10, 200, 20, 50]
dists = [np.random.normal(0, 1, n) for n in lengths]

fig, ax = plt.subplots()
ax.boxplot(dists, patch_artist=True)
plt.show()

enter image description here