Python barh绘图失败,出现“错误”;ValueError:大小不兼容:参数';宽度';长度必须为2或标量;
我希望一行有4个绘图,因此我尝试将Python barh绘图失败,出现“错误”;ValueError:大小不兼容:参数';宽度';长度必须为2或标量;,python,matplotlib,bar-chart,Python,Matplotlib,Bar Chart,我希望一行有4个绘图,因此我尝试将ax插入列表并循环遍历该列表。每个子图应类似于以下图: df.plot(kind="barh") 但是,以下代码不起作用: df = pd.DataFrame({'a': [1, 2],'b': [3, 4]}) df.index = ["Row1", "Row2"] ax1 = fig.add_subplot(1, 4, 1) ax2 = fig.add_subplot(1, 4, 2) ax3 = fig.add_subplot(1, 4, 3) ax4
ax
插入列表
并循环遍历该列表。每个子图应类似于以下图:
df.plot(kind="barh")
但是,以下代码不起作用:
df = pd.DataFrame({'a': [1, 2],'b': [3, 4]})
df.index = ["Row1", "Row2"]
ax1 = fig.add_subplot(1, 4, 1)
ax2 = fig.add_subplot(1, 4, 2)
ax3 = fig.add_subplot(1, 4, 3)
ax4 = fig.add_subplot(1, 4, 4)
axis_list = [ax1, ax2, ax3, ax4]
for ax in axis_list:
ax.barh(df, kind='barh', width=0.8, colormap='Set1')
它失败,但出现以下异常:
ValueError:大小不兼容:参数“宽度”必须为长度2或标量
您可以使用plt.subplot命令创建轴数组,这将整理代码。还请注意,您可以使用df.plot并指定要打印的轴
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'a': [1, 2],'b': [3, 4]})
df.index = ["Row1", "Row2"]
fig, axis_list = plt.subplots(1,4)
for ax in axis_list:
df.plot(kind='barh',ax=ax,width=0.8, colormap='Set1')
fig.show()