python - 使用 matplotlib 在矩阵中绘制箱线图

标签 python python-3.x numpy matplotlib

需要获取两列中的 24 个箱线图(每行 12 个)并将它们打印到 postscript 文件中。

此代码引发错误

def do(self, ...):
    axarray = plt.subplots(12, 3, figsize=(11.69,8.27))
    for i in range(2):
        for month in range(12):
            monthBoxplot = [    [ 2, 2, 2 ], 
                                [ 2, 2, 2 ] ]
            axarray[i, m].boxplot(monthBoxplot)
    ax.imshow(axarray, aspect='normal')
    plt.savefig("/asdasd"+str(year)+".ps")

错误

axarray[i,m].boxplot(boxplotMensual)

TypeError: tuple indices must be integers, not tuple

这意味着 [i,m] 是一个元组...对吗?我该怎么办? 谢谢

最佳答案

类似的东西

f, axarray = plt.subplots(12, 3, figsize=figsize)
for i in range(2):
    for month in range(12):
        axarray[i, month].boxplot(data)

应该做。

有很多子图示例可用 here .

编辑:实际上f没有被使用,你可以在第一行用_替换f。但如果您不输入 f,则 axarray 不会分配正确的对象。为了简单起见,您能看出 a1, a2 = 3, 4a2 = 3, 4 之间的区别吗?在第二种情况下,a2是一个元组,如果你在代码中执行a2[0, 0](即axarray[i, m]),当axarray是一个tuple),您会收到错误,因为您无法使用元组索引元组。

关于python - 使用 matplotlib 在矩阵中绘制箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783673/

相关文章:

python - Numpy 将函数应用于数组

python - 获取类型错误 : reduction operation 'argmax' not allowed for this dtype when trying to use idxmax()

python - 将 Pandas DataFrame 转换为 JSON 作为更大数据结构的元素

python - Tensorflow:子张量的reduce_mean并连接结果

python-3.x - Python 与 BigQuery FarmHash 有时不相等

python - numpy 字符串数组索引行为

python - 需要基本的 Python 编程帮助 : Arrays and random locations

python - 这个 bool 测试 python 是否用于将列表成员限制为一组合法值?

python - django 管理错误 - 'django_content_type.name' 中的未知列 'field list'

python - Pyqt5 鼠标事件不适用于我的自定义选项卡栏