首页 > 解决方案 > Matplotlib:如何更改双条形图的 figsize

问题描述

我使用以下代码在 matplotlib 中绘制了一个双条形图:

x = pd.Series(range(12))
y = self.cust_data['Cluster_ID'].value_counts().sort_index()
z = self.cust_data['Cluster_ID_NEW'].value_counts().sort_index()
plt.bar(x + 0.0, y, color = 'b', width = 0.5)
plt.bar(x + 0.5, z, color = 'g', width = 0.5)
plt.legend(['Old Cluster', 'New Cluster'])
plt.savefig("C:\\Users\\utkarsh.a.ranjan\\Documents\\pyqt_data\\generate_results\\bar", bbox_inches='tight',pad_inches=0.1)
plt.clf()

我想使用 figsize 参数使结果图的大小更大。绘制单个条形图时这很容易,但在这里我对放置 figsize 参数的位置感到困惑。

标签: pythonmatplotlib

解决方案


你可以设置大小figsize

import matplotlib.pyplot as plt

f, ax = plt.subplots(figsize=(18,5)) # set the size that you'd like (width, height)
plt.bar([1,2,3,4], [0.1,0.2,0.3,0.4], label = 'first bar')
plt.bar([10,11,12,13], [0.4,0.3,0.2,0.1], label = 'second bar')
ax.legend(fontsize = 14)

在此处输入图像描述


推荐阅读