我已经使用以下代码在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()
Run Code Online (Sandbox Code Playgroud)
我想使用figsize参数使结果图的尺寸更大。当绘制单个条形图时,这很容易,但是在这里,我对放置figsize参数的位置感到困惑。
最好的方法是遵循更面向对象的方法:
fig, ax = plt.subplots(figsize=(12,12))
ax.bar(x + 0.0, y, color = 'b', width = 0.5)
ax.bar(x + 0.5, z, color = 'g', width = 0.5)
ax.legend(['Old Cluster', 'New Cluster'])
fig.savefig("C:\\Users\\utkarsh.a.ranjan\\Documents\\pyqt_data\\generate_results\\bar", bbox_inches='tight',pad_inches=0.1)
plt.clf()
Run Code Online (Sandbox Code Playgroud)
您可能还想为文件名添加格式。如果不是,则格式取自您的 rc 参数savefig.format,通常是png.
顺便说一句,如果你想尽可能地坚持你的代码,你也可以在你的之前添加一行plt.bar(...):
plt.figure(figsize=(12,12))
Run Code Online (Sandbox Code Playgroud)
您可以使用 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13115 次 |
| 最近记录: |