我正在学习一本介绍Matplotlib的教科书中的一个例子。下面的代码在通过命令行Python运行时保存一个图形,但在通过Jupyter运行时保存一个空图像。为什么会发生这种情况?
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x), '--')
fig.savefig('image.png')发布于 2020-11-05 00:07:36
我尝试在Jupyter中运行相同的代码,但没有您的第一行代码:
#%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x), '--')
fig.savefig(r'C:\Users\new_folder\image.png')确保为输出图像指定了正确的路径。这种方式对我来说工作得很好,我得到了.png镜像
https://stackoverflow.com/questions/64683500
复制相似问题