gpt4 book ai didi

python - 为什么 matplotlib 需要在 plt.scatter() 之前设置日志比例而不是 plt.plot()?

转载 作者:IT老高 更新时间:2023-10-28 20:56:03 25 4
gpt4 key购买 nike

我在这个 helpful answer 中发现了当在 y 轴上使用对数刻度时,plt.scatter()plt.plot() 的行为不同。

使用 plot,我可以在使用 plt.show() 之前的任何时间更改为日志,但必须预先设置日志,之前 使用了 scatter 方法。

这只是 matplotlib 中的历史和不可逆转的工件,还是属于“意外行为”类别?

enter image description here

import matplotlib.pyplot as plt

X = [0.997, 2.643, 0.354, 0.075, 1.0, 0.03, 2.39, 0.364, 0.221, 0.437]
Y = [15.487507, 2.320735, 0.085742, 0.303032, 1.0, 0.025435, 4.436435,
0.025435, 0.000503, 2.320735]

plt.figure()

plt.subplot(2,2,1)
plt.scatter(X, Y)
plt.xscale('log')
plt.yscale('log')
plt.title('scatter - scale last')

plt.subplot(2,2,2)
plt.plot(X, Y)
plt.xscale('log')
plt.yscale('log')
plt.title('plot - scale last')

plt.subplot(2,2,3)
plt.xscale('log')
plt.yscale('log')
plt.scatter(X, Y)
plt.title('scatter - scale first')


plt.subplot(2,2,4)
plt.xscale('log')
plt.yscale('log')
plt.plot(X, Y)
plt.title('plot - scale first')


plt.show()

最佳答案

这在某种程度上与 matplotlib 计算的显示区域(轴限制)有关。

通过使用 set_xlimset_ylim 方法手动编辑坐标区范围来修复此行为。

plt.figure()
plt.scatter(X, Y)
plt.yscale('log')
plt.xscale('log')
axes = plt.gca()
axes.set_xlim([min(X),max(X)])
axes.set_ylim([min(Y),max(Y)])
plt.show()

Image

但是,我还没有弄清楚这种行为的确切原因。欢迎提出建议。

编辑

如评论部分所述,显然 Matplotlib 已识别 Autoscaling has fundamental problems作为其官方 Github 存储库中的发布关键问题,将在即将发布的版本中修复。谢谢。

关于python - 为什么 matplotlib 需要在 plt.scatter() 之前设置日志比例而不是 plt.plot()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38800189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com