Python matplotlib默认在 Axis 外绘制曲线?

标签 python matplotlib plot axis

如果应用 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”。这就是为什么我首先认为更新解决了问题。 enter image description here

当我选择“SVG”时,线条仍然绘制在 Axis 外。我执行了

plt.plot([1,2,3,4,5,6,7]), plt.xlim((2, 3))

enter image description here 但是,当我选择“PNG”时,一切正常。

我会忽略这个错误,因为当我选择“SVG”时,执行

plt.savefig("myfig.pdf")

然后打开文件,所有行都在框中。

关于Python matplotlib默认在 Axis 外绘制曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74704689/

相关文章:

python - 在 while 循环条件中分配给函数返回值并捕获异常

python - 用数字和字符串数据绘制直方图?

r - 如何绘制空条形图?

R:从用户绘制的点生成坐标数据?

plot - 如何在 Julia 中使用 Gadfly.jl 绘制线性函数?

python - 使用 scikit-learn 对随机森林进行递归特征消除

Python Mixin - 未解析的属性引用 [PyCharm]

python - 关于检测usb设备的问题

python - Matplotlib plt.xlabel() 与 ax.set_xlabel()

python - 绘图窗口不显示 - Tkinter 存在,但未找到头文件。