Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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 barh绘图失败,出现“错误”;ValueError:大小不兼容:参数';宽度';长度必须为2或标量;_Python_Matplotlib_Bar Chart - Fatal编程技术网

Python barh绘图失败,出现“错误”;ValueError:大小不兼容:参数';宽度';长度必须为2或标量;

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

我希望一行有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 = 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()