我正在尝试创建箱线图,但是在尝试理解语法时遇到了问题。现在,这是我的代码
mydata = np.array([data['Adult_Card'], (data2[(data2['Fare_Type']=='Adult card fare') & (data2['Applicable_Time']=='All other timings')]['Fare_per_Ride']),
(data2[(data2['Fare_Type']=='Adult card fare') & (data2['Applicable_Time']=='Before 7.45am (Weekdays excluding public holidays)')]['Fare_per_Ride']),
(data2[(data2['Fare_Type']=='Single trip') & (data2['Applicable_Time']=='All timings')]['Fare_per_Ride']),
(data['Adult_Cash'])])
labels = np.array(["Adult Card(Bus)","Adult Card(MRT)","Adult Card(Before 7.45am MRT)","Single Trip", "Adult Cash"])
fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(111)
ax1.set_xticklabels(labels, rotation='vertical')
plt.title(title)
plt.xlabel('Distance(Km)')
plt.ylabel('Fares')
plt.legend(loc='upper left');
print(mydata)
plt.boxplot(mydata,labels=labels)
plt.show()
这是mydata的值
[[ 83 93 103 113 122 129 135 139 143 147 151 155 159 163 167 171 175 178
181 184 187 189 191 193 194 195 196 197 198 199 200 201 202 203 204 205
206 207 208]
[ 83 93 103 113 122 129 135 139 143 147 151 155 159 163 167 171 175 178
181 184 187 189 191 193 194 195 196 197 198 199 200 201 202 203 204 205
206 207 208]
[ 33 43 53 63 72 79 85 89 93 97 101 105 109 113 117 121 125 128
131 134 137 139 141 143 144 145 146 147 148 149 150 151 152 153 154 155
156 157 158]
[150 170 170 170 190 190 190 210 210 230 230 230 230 240 240 240 240 250
250 250 250 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260
260 260 260]
[150 170 170 170 190 190 190 210 210 230 230 230 230 240 240 240 240 250
250 250 250 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260
260 260 260]]
我想通过array元素创建箱形图,所以
[ 83 93 103 113 122 129 135 139 143 147 151 155 159 163 167 171 175 178
181 184 187 189 191 193 194 195 196 197 198 199 200 201 202 203 204 205
206 207 208]
将是第一个框,随后是。我对matplotlib相当陌生,所以我真的不知道出了什么问题。这是错误
ValueError Traceback (most recent call last)
<ipython-input-29-31b9e5335060> in <module>
32 print("Original data: " + str(data2.shape))
33 print(mydata)
---> 34 plt.boxplot(mydata,labels=labels)
35 plt.show()
~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in boxplot(x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_xticks, autorange, zorder, data)
2494 whiskerprops=whiskerprops, manage_xticks=manage_xticks,
2495 autorange=autorange, zorder=zorder, **({"data": data} if data
-> 2496 is not None else {}))
2497
2498
~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.__name__),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.__doc__ = _add_data_doc(inner.__doc__,
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in boxplot(self, x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_xticks, autorange, zorder)
3501
3502 bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
-> 3503 labels=labels, autorange=autorange)
3504 if notch is None:
3505 notch = rcParams['boxplot.notch']
~\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in boxplot_stats(X, whis, bootstrap, labels, autorange)
1179 labels = itertools.repeat(None)
1180 elif len(labels) != ncols:
-> 1181 raise ValueError("Dimensions of labels and X must be compatible")
1182
1183 input_whis = whis
ValueError: Dimensions of labels and X must be compatible
更新:我从数据中删除了np.array(),并且工作正常。我对此没有任何解释。
答案 0 :(得分:1)
编辑:
我不知为何错过了mydata
是一个numpy数组而不是列表列表的事实...
要使代码与numpy数组一起使用,应使用mydata的转置,如下所示:
###
plt.boxplot(mydata.T, labels=labels)
###
下面列出的普通帖子
某处您的错误报告与您的代码不一致。如果我运行以下代码,我肯定会得到箱线图。
import numpy as np
from matplotlib import pyplot as plt
mydata = [
[ 83, 93, 103, 113, 122, 129, 135, 139, 143,
147, 151, 155, 159, 163, 167, 171, 175, 178,
181, 184, 187, 189, 191, 193, 194, 195, 196,
197, 198, 199, 200, 201, 202, 203, 204, 205,
206, 207, 208],
[ 83, 93, 103, 113, 122, 129, 135, 139, 143,
147, 151, 155, 159, 163, 167, 171, 175, 178,
181, 184, 187, 189, 191, 193, 194, 195, 196,
197, 198, 199, 200, 201, 202, 203, 204, 205,
206, 207, 208],
[ 33, 43, 53, 63, 72, 79, 85, 89, 93, 97, 101,
105, 109, 113, 117, 121, 125, 128, 131, 134,
137, 139, 141, 143, 144, 145, 146, 147, 148,
149, 150, 151, 152, 153, 154, 155, 156, 157,
158],
[150, 170, 170, 170, 190, 190, 190, 210, 210,
230, 230, 230, 230, 240, 240, 240, 240, 250,
250, 250, 250, 260, 260, 260, 260, 260, 260,
260, 260, 260, 260, 260, 260, 260, 260, 260,
260, 260, 260],
[150, 170, 170, 170, 190, 190, 190, 210, 210,
230, 230, 230, 230, 240, 240, 240, 240, 250,
250, 250, 250, 260, 260, 260, 260, 260, 260,
260, 260, 260, 260, 260, 260, 260, 260, 260,
260, 260, 260]
]
labels = np.array(["Adult Card(Bus)", "Adult Card(MRT)", "Adult Card(Before 7.45am MRT)", "Single Trip", "Adult Cash"])
fig = plt.figure(figsize=(20, 10))
ax1 = fig.add_subplot(111)
ax1.set_xticklabels(labels, rotation='vertical')
plt.title('title')
plt.xlabel('Distance(Km)')
plt.ylabel('Fares')
plt.legend(loc='upper left');
print(mydata)
plt.boxplot(mydata, labels=labels)
plt.show()
还有一些其他事情是不正确的,例如,我得到警告,“没有发现带有图例的带有标签的句柄。”但是也许最好在另一个问题中提出来。
答案 1 :(得分:0)
boxplot
文档说明
为x的每一列或序列x中的每个向量绘制箱形图。
因此,需要区分两种情况
为序列(即元组,列表)的每个向量(即元组,列表,数组)生成框。
import numpy as np
import matplotlib.pyplot as plt
data = [[11, 21, 31, 41], # box 1
[12, 22, 32, 42], # box 2
[13, 23, 33, 43]] # box 3
plt.boxplot(data)
plt.show()
这将产生3个框,每个框包含每个内部列表的数据。 它允许具有不同长度的向量
data = [[11, 21, 31, 41, 51, 61],
[12, 22, 32],
[13, 23, 33, 43]]
如果输入是一个numpy数组,则将按列进行解释,即2D数组中的每一列都有其自己的框。
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[11, 21, 31, 41],
[12, 22, 32, 42],
[13, 23, 33, 43]])
# ^ ^ ^ ^
# box 1 2 3 4
plt.boxplot(data)
plt.show()