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

Matplotlib中plt.show()显示空白绘图窗口问题求助

解决Matplotlib弹出空白窗口的问题

你的问题出在fig.add_axes([1, 1, 1, 1])这行代码上。add_axes()的参数格式是[left, bottom, width, height],四个值都是基于画布的相对位置和大小,范围必须在0到1之间。你设置的[1,1,1,1]把坐标轴完全放在了画布外面,自然看不到图形。

修正后的代码

# Python program to show pyplot module
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
 
# 创建宽5英寸、高4英寸的画布
fig = plt.figure(figsize =(5, 4))
 
# 修改add_axes参数,将坐标轴放在画布可视范围内
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
 
# 绘制数据
ax.plot([2, 3, 4, 5, 5, 6, 6],
        [5, 7, 1, 3, 4, 6 ,8])
plt.show()

参数[0.1, 0.1, 0.8, 0.8]的意思是:从画布左下角(坐标原点)向右偏移10%、向上偏移10%的位置开始,创建一个占画布80%宽度和高度的坐标轴,这样图形就能正常显示在窗口里了。

另外,也可以用更简洁的方式创建画布和坐标轴,代码可读性更高:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(5,4))
ax.plot([2, 3, 4, 5, 5, 6, 6], [5, 7, 1, 3, 4, 6 ,8])
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 08:42:49