Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib子地块箱线图隐藏某些轴标签_Python_Matplotlib_Plot - Fatal编程技术网

Python matplotlib子地块箱线图隐藏某些轴标签

Python matplotlib子地块箱线图隐藏某些轴标签,python,matplotlib,plot,Python,Matplotlib,Plot,我想迭代列表并绘制每个列表的箱线图。因为数据不能全部放入内存,所以我无法指定要绘制的预定义数量的箱线图,所以我使用subplot函数以迭代方式添加图 我的问题是没有将轴标签添加到带有箱线图的绘图中,而只显示最后一个标签。如何使用子图迭代标记箱线图 下面是我想做的一个简单例子。虽然在现实中,我实际上是在循环中循环使用同一个列表,而不是在列表列表上迭代,但它可以说明问题。可以看出,在yaxis中仅设置了“lb”,并且对于第一个底部箱线图,该图未显示“la” 谢谢 %matplotlib inline

我想迭代列表并绘制每个列表的箱线图。因为数据不能全部放入内存,所以我无法指定要绘制的预定义数量的箱线图,所以我使用subplot函数以迭代方式添加图

我的问题是没有将轴标签添加到带有箱线图的绘图中,而只显示最后一个标签。如何使用子图迭代标记箱线图

下面是我想做的一个简单例子。虽然在现实中,我实际上是在循环中循环使用同一个列表,而不是在列表列表上迭代,但它可以说明问题。可以看出,在yaxis中仅设置了“lb”,并且对于第一个底部箱线图,该图未显示“la”

谢谢

%matplotlib inline
import matplotlib.pyplot as plt

la = [24, 28, 31, 34, 38, 40, 41, 42, 43, 44]
lb = [5, 8, 10, 12, 15, 18, 21, 25, 30, 39]
names = ['la', 'lb']
myList = [la] + [lb]
myList

# set fig for boxplots
fig, ax = plt.subplots(sharex=True)
# Add a horizontal grid to the plot
ax.xaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5)
ax.set_axisbelow(True)
ax.set_title('Some Title')

for i,l in enumerate(myList):
    ax.boxplot(l, vert=False, positions = [i])
    ax.set_yticklabels([names[i]])

ax.set_ylim(-0.5, len(myList)-0.5)

在循环内设置标签将覆盖以前的标签。因此,标签应该设置在循环之外。您还需要确保两个标签都有刻度

因此,一个解决办法是添加

ax.set_yticks(range(len(myList)))
ax.set_yticklabels(names)
在循环之外

完整代码:

import matplotlib.pyplot as plt

la = [24, 28, 31, 34, 38, 40, 41, 42, 43, 44]
lb = [5, 8, 10, 12, 15, 18, 21, 25, 30, 39]
names = ['la', 'lb']
myList = [la] + [lb]
myList

# set fig for boxplots
fig, ax = plt.subplots(sharex=True)
# Add a horizontal grid to the plot
ax.xaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5)
ax.set_axisbelow(True)
ax.set_title('Some Title')

for i,l in enumerate(myList):
    ax.boxplot(l, vert=False, positions = [i])

ax.set_yticks(range(len(myList)))
ax.set_yticklabels(names)

ax.set_ylim(-0.5, len(myList)-0.5)

plt.show()

太好了。我以前也尝试过类似的方法,但没有设置刻度,结果与我的问题相同。因此,我将引用你的一句话“你还需要确保两个标签上都有记号。”这是做这件事的必要条件。确实如此。完成。