python - 在循环内增加 plt.subplot() 中绘图的 h 大小 - Python
问题描述
我有这个代码:
for i in ["Dia", "DiaSemana", "Mes", "Año", "Feriado"]:
plt.subplot(1,2,1)
sns.boxplot(x=i, y="Y", data=df)
plt.subplot(1,2,2)
sns.boxplot(x=i, y="Temp", data=df)
plt.tight_layout()
plt.show()
它给了我我需要的所有情节。这是一次性循环:
如您所见,它们x axis
是重叠的,我正在尝试增加每个绘图的水平尺寸以获得更好的可视化效果。
解决方案
您受到图形宽度的限制。figsize
您可以使用该属性使您的身材更宽。您可以通过显式定义 ( plt.figure
) 或获取当前图形 ( plt.gcf
) 来“抓取”您的图形。
但是,我更喜欢使用plt.subplots
来定义图形和轴:
for i in ["Dia", "DiaSemana", "Mes", "Año", "Feriado"]:
fig, axes = plt.subplots(ncols=2, figsize=(15, 5)) # set width of figure and define both figure and axes
sns.boxplot(x=i, y="Y", data=df, ax=axes[0])
sns.boxplot(x=i, y="Temp", data=df, ax=axes[1])
plt.tight_layout()
plt.show()
或者,您可以减少 x 轴上的刻度数。
推荐阅读
- java - Spring Config 客户端看到 Spring Config Server 的大量 CLOSE_WAIT
- google-apps-script - Google Script Trigger Collapse Group 问题
- xamarin - 在 MvvmCross 的 TabbedPage 中的选项卡内导航
- swiftui - 是否有任何选项可以设置 SwiftUI 中菜单项的顺序?
- c# - 根据另一个集合中列出的类型创建元素集合
- docker - 将letsencrypt文件复制到docker容器
- r - 在 R 中使用完整的数据集时出现 pointDistance() 错误
- seo - json+ld 脚本不显示有效或验证失败
- c# - 如何使用 OAuth2 和 RestSharp 存储 accessToken
- ruby-on-rails - ROR:安装 libv8 (3.16.14.19) 时出错,Bundler 无法继续