matplotlib - 绘制和保存多个图表时撤消 plt.gcf().subplots_adjust
问题描述
在单个 Python 脚本中绘制连续图表时,plt.clf()
不会反转plt.gcf().subplots_adjust(bottom=0.5)
. 第一个图表进行了调整以允许 x 轴标签有更多的显示空间,但此更改持续到第二个图表。如何绘制第二张图表以dist()
使其看起来正常?即使在函数中或调用之后调用sns.set()
或重新导入似乎也无法解决问题。plt
dist()
box_adj()
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
def box_adj():
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", hue="smoker",
data=tips, palette="Set3")
plt.gcf().subplots_adjust(bottom=0.5)
plt.savefig('box.png')
plt.clf()
def dist():
ax = sns.distplot(np.random.randn(1000), kde=False)
plt.savefig('dist.png')
plt.clf()
if __name__ == "__main__":
box_adj()
dist()
解决方案
plt.clf()
只清除情节图的内容,使图保持原样。它的空白设置和大小不会改变。
您可以替换plt.clf()
为plt.close()
完全关闭图形(matplotlib 将在需要时自动创建一个新图形)。或者,您可以显式调用fig = plt.figure(...)
或fig, ax = plt.subplots(...)
使用默认设置创建一个新图形。
推荐阅读
- python - 旋转 Pandas 表 - 特殊问题
- python - Tensorflow tf.data.Dataset 使用 map 函数时出错 | 键错误
- php - PHP中的PdfParser问题
- python - Django模型-如何通过相关模型使用过滤器
- html - Xpath在tbody内按位置4阶td查找文本并单击同一行中的编辑
- c++ - C++ 将原始 std::bytes 复制到 std::vector
- javascript - 如何在javascript中的一个选择下拉列表中显示所有父值和子值
- python - 带有rest框架的django在线用户
- oracle - Oracle Application Express 的问题
- google-cloud-platform - ESP32 到 GCP IOT Core 连接失败