You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改输出Matplotlib Axes对象的函数生成的图形尺寸?

解决PySAL QStatistic.plot()图形压缩及Axes绑定问题

核心原因

你遇到的'Axes must have been created in the present figure'错误,是因为Matplotlib的Axes对象天生绑定到创建它的Figure,无法直接将一个已有Axes添加到另一个新Figure中。

两种可行解决方案

方案1:提前创建指定尺寸的Figure,让plot()在该Axes上绘制

这是最稳妥的方式,直接指定绘图的画布尺寸:

import matplotlib.pyplot as plt
from pointpats import QStatistic

# 初始化你的QStatistic实例(替换成你的数据)
qstat = QStatistic(your_points, your_window)

# 创建指定宽高的Figure和Axes,比如(10,6)是宽10英寸、高6英寸
fig, ax = plt.subplots(figsize=(10, 6))
# 让QStatistic的plot方法在这个Axes上生成图形
qstat.plot(ax=ax)
plt.show()

方案2:修改已有Axes所属Figure的尺寸

如果已经生成了Axes对象,直接修改它绑定的Figure尺寸即可:

# 先调用plot得到Axes对象
ax = qstat.plot()
# 获取该Axes所属的Figure
fig = ax.get_figure()
# 设置Figure的尺寸
fig.set_size_inches(12, 8)
plt.show()

内容的提问来源于stack exchange,提问作者Rhea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 08:47:38