在python中使用plt.subplot清理代码
我有以下数据,在python中使用plt.subplot清理代码,python,pandas,dataframe,matplotlib,Python,Pandas,Dataframe,Matplotlib,我有以下数据,df OrderCount OrderAvgSize AvgDeliverCost AvgOrderValue count Response AmznPrime 12.41 11.46 4.45 10.13 392 Both_Or_None 17.20 6.85
df
OrderCount OrderAvgSize AvgDeliverCost AvgOrderValue count
Response
AmznPrime 12.41 11.46 4.45 10.13 392
Both_Or_None 17.20 6.85 4.07 5.18 1099
Walmart+ 24.75 13.45 4.57 9.61 687
我使用它创建了一个子地块,代码如下:
fig, ax = plt.subplots(1,5,sharex=True,figsize=(20,5),constrained_layout=True)
ax1,ax2,ax3,ax4,ax5 = ax.flatten()
sns.barplot(x=df.index, y=df.OrderCount, ax=ax1, data=df)
sns.barplot(x=df.index, y=df.OrderAvgSize, ax=ax2, data=df)
sns.barplot(x=df.index, y=df.AvgDeliverCost, ax=ax3, data=df)
sns.barplot(x=df.index, y=df.AvgOrderValue, ax=ax4, data=df)
sns.barplot(x=df.index, y=df['count'], ax=ax5, data=df)
axs = [ax1,ax2,ax3,ax4,ax5]
for i in axs:
i.grid(zorder=0,alpha=.4)
i.set_axisbelow(True)
我的问题是:有没有办法进一步清理这个代码?我觉得应该有一种方法可以使用for循环来绘制每个子批并消除一些行,但我不确定如何使用ax
和y=
在每一行中都是不同的。同样,我在生成子地块方面没有问题,但我觉得这样做可能效率低下
谢谢 这个
fig, ax = plt.subplots(1,5,sharex=True,figsize=(20,5),constrained_layout=True)
ax = ax.flatten()
col = ["OrderCount", "OrderAvgSize", "AvgDeliverCost", "AvgOrderValue", "count"]
for a, c in zip(ax, col):
sns.barplot(x=df.index, y=df[c], ax=a, data=df)
a.grid(zorder=0,alpha=.4)
a.set_axisbelow(True)
这个
您可以在df.column上使用
enumerate
。我以前从未使用过enumerate,您能解释一下吗?谢谢您可以在df.column上使用enumerate
。我以前从未使用过enumerate,您能解释一下吗?谢谢