为什么matplotlib要求在plt.scatter()之前而不是plt.plot()设置日志比例? [英] Why does matplotlib require setting log scale before plt.scatter() but not plt.plot()?

查看:39
本文介绍了为什么matplotlib要求在plt.scatter()之前而不是plt.plot()设置日志比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此

 将matplotlib.pyplot导入为pltX = [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('散布-最后缩放')plt.subplot(2,2,2)plt.plot(X,Y)plt.xscale('log')plt.yscale('log')plt.title('情节 - 最后缩放')plt.subplot(2,2,3)plt.xscale('log')plt.yscale('log')plt.scatter(X, Y)plt.title('分散 - 先缩放')plt.subplot(2,2,4)plt.xscale('log')plt.yscale('log')plt.plot(X, Y)plt.title('情节 - 先缩放')plt.show()

解决方案

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

通过使用 set_xlim set_ylim 方法手动编辑轴范围可以解决此问题.

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

然而,我还没有弄清楚这种行为的确切原因.欢迎提出建议.

编辑

如评论部分所述,显然 Matplotlib 已确定 自动缩放存在根本问题他们官方 Github 存储库中的 Release Critical Issue,将在即将发布的版本中修复.谢谢.

I found out in this helpful answer that plt.scatter() and plt.plot() behave differently when a logrithmic scale is used on the y axis.

With plot, I can change to log any time before I use plt.show(), but log has to be set up-front, before the scatter method is used.

Is this just a historical and irreversible artifact in matplotlib, or is this in the 'unexpected behavior' category?

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()

解决方案

This somehow has to do with the the display area (axes limits) calculated by matplotlib.

This behaviour is fixed by manually editing the axes range by using set_xlim and set_ylim methods.

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()

The exact reason of this behavior is however not yet figured out by me. Suggestions are welcomed.

EDIT

As mentioned in comments section, apparently Matplotlib has identified Autoscaling has fundamental problems as a Release Critical Issue on their official Github repo, which would be fixed in upcoming versions. Thanks.

这篇关于为什么matplotlib要求在plt.scatter()之前而不是plt.plot()设置日志比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆