Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 如何使用matplotlib从带有Pandas的未堆叠数据框绘制多个图表_Python 3.x_Pandas_Matplotlib - Fatal编程技术网

Python 3.x 如何使用matplotlib从带有Pandas的未堆叠数据框绘制多个图表

Python 3.x 如何使用matplotlib从带有Pandas的未堆叠数据框绘制多个图表,python-3.x,pandas,matplotlib,Python 3.x,Pandas,Matplotlib,这是我使用以下代码获得的数据集示例 ComplaintCity = nyc_df.groupby(['City','Complaint Type']).size().sort_values().unstack() top5CitiesByComplaints = ComplaintCity[top5Complaints].rename_axis(None, axis=1) top5CitiesByComplaints 车道堵塞非法停车噪音-街道/人行道噪音-商用废弃车辆 城市 阿凡纳35.0

这是我使用以下代码获得的数据集示例

ComplaintCity = nyc_df.groupby(['City','Complaint Type']).size().sort_values().unstack()
top5CitiesByComplaints = ComplaintCity[top5Complaints].rename_axis(None, axis=1)
top5CitiesByComplaints
车道堵塞非法停车噪音-街道/人行道噪音-商用废弃车辆 城市 阿凡纳35.0 58.0 29.0 2.0 27.0 ASTORIA 2734.0 1281.0 500.0 1554.0 363.0 海湾边377.0 514.0 15.0 40.0 198.0 贝勒罗斯95.0 106.0 13.0 37.0 89.0 微风点3.0 15.0 1.0 4.0 3.0 布朗克斯12754.0 7859.0 8890.0 2433.0 1952.0 布鲁克林28147.0 27461.0 13354.0 11458.0 5179.0 坎布里亚高地147.0 76.0 25.0 12.0 115.0 中央公园南2.0 95.0南南 大学点435.0352.033.035.0184.0 电晕2761.0660.0238.0248.0 我希望能够为每个投诉绘制与水平条形图相同的图表。它应该显示投诉最多的城市。类似于下图的内容。我不知道该怎么办


您可以创建一个包含
子地块的轴实例列表
,并逐个绘制列:

fig, axes = plt.subplots(3,2,figsize=(10,6))
for c,ax in zip(df.columns, axes.ravel()):
    df[c].sort_values().plot.barh(ax=ax)

fig.tight_layout()
然后你会得到这样的结果:


可能也应该在排序值之前执行dropna()?由OP选择是否绘制
nan
值,但这是一个好建议。谢谢,太棒了。最后一件事是如何添加标题。说出哪个图表属于哪个投诉添加
ax。在for循环中设置标题(c)
fig, axes = plt.subplots(3,2,figsize=(10,6))
for c,ax in zip(df.columns, axes.ravel()):
    df[c].sort_values().plot.barh(ax=ax)

fig.tight_layout()