[英]Matplotlib subplots are all the same size ... how to set them up so subplots scale including the xticks?
[英]Matplotlib set width of bars to be the same size for all subplots
我的下图中有3个子图。 我将每个子图的宽度设置为0.3,但是条形图的大小现在不均。 如何使条形尺寸相同,同时在成对的条形之间保持间距?
我的代码:
g=['col1','col2','col3']
fig, axs = plt.subplots(1,len(g)/1,figsize = (50,20))
axs = axs.ravel()
for j,x in enumerate(g):
df_plot[x].value_counts(normalize=True).head().plot(kind='bar',ax=axs[j],position = 0, title = 'mytitle', fontsize = 30, width=0.3)
df_plot2[x].value_counts(normalize=True).head().plot(kind='bar',ax=axs[j],position = 1, color='red', width=0.3)
axs[j].title.set_size(40)
fig.tight_layout()
如果所有子图都具有相同的大小,则其想法是设置x轴的限制,以使所有条具有相同的宽度。
ax.set_xlim(-0.5,maxn-0.5)
其中maxn
是要绘制的最大条形数量。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
col1 = np.random.choice(["Mon", "Tue", "Wed", "Thu", "Fri"], 100, p=[0.1, 0.4, 0.2, 0.2,0.1])
col2 = np.random.choice([0,1], 100, p=[0.4, 0.6])
col3 = np.random.choice(list("abcde"), 100, p=[0.15, 0.35, 0.1, 0.3,0.1])
df = pd.DataFrame({'col1':col1,'col2':col2,'col3':col3})
g=['col1','col2','col3']
fig, axs = plt.subplots(1,len(g)/1,figsize = (10,4))
axs = axs.ravel()
maxn = 5
for j,x in enumerate(g):
df[x].value_counts(normalize=True).head().plot(kind='bar',ax=axs[j],position = 0, title = 'mytitle', width=0.3)
df[x].value_counts(normalize=True).head().plot(kind='bar',ax=axs[j],position = 1, color='red', width=0.3)
axs[j].set_xlim(-0.5,maxn-0.5)
fig.tight_layout()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.