**
matplotlib报错ValueError: height and width must be > 0解决方案
**
问题案例
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.show()
运行以上画图代码,报错ValueError: height and width must be > 0
问题原因
有些远程软件自动会分配虚拟屏幕,matplotlib没有物理屏幕,plt.show()会报错ValueError: height and width must be > 0
解决方案
1、有物理屏幕的显示器,设置为单屏显示,以ubantu为例
2、无屏幕的服务器,修改代码,加上matplotlib.use(‘Agg’)和plt.savefig(‘./test.jpg’),去掉plt.show(),将绘图保存为图片。
代码修改如下:
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.savefig('./test.jpg')