如果应用 matplotlib.pyplot.xlim
,Python matplotlib 默认情况下会在 Axis 外绘图吗?
这是我写的代码。
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.linspace(-10, 10, 11)
y = x**2
plt.plot(x, y)
plt.xlim((-5, 5))
这就是我得到的:enter image description here
这就是我想要的: enter image description here
我正在使用 matplotlib 版本 3.5.3 和 Spyder IDE。
最佳答案
我将 Spyder 从 5.1.5 更新到 5.3.3
conda uninstall spyder
conda install spyder=5.3.3
现在 Spyder 不再跨 Axis 绘制。
编辑: 我错了!问题仍然出现。我发现 Spyder IPython 控制台格式选项导致了错误。默认设置为“PNG”。我通常使用“SVG”。这就是为什么我首先认为更新解决了问题。
当我选择“SVG”时,线条仍然绘制在 Axis 外。我执行了
plt.plot([1,2,3,4,5,6,7]), plt.xlim((2, 3))
我会忽略这个错误,因为当我选择“SVG”时,执行
plt.savefig("myfig.pdf")
然后打开文件,所有行都在框中。
关于Python matplotlib默认在 Axis 外绘制曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74704689/