简体   繁体   English

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

[英]Why does matplotlib require setting log scale before plt.scatter() but not plt.plot()?

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. 我在这个有用的答案中发现,当在y轴上使用plt.plot()plt.scatter()plt.plot()行为会有所不同。

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. 使用plot ,我可以在使用plt.show()之前随时更改为log,但是使用scatter方法之前必须预先设置log。

Is this just a historical and irreversible artifact in matplotlib, or is this in the 'unexpected behavior' category? 这只是matplotlib中的一个历史性且不可逆转的工件,还是属于“意外行为”类别?

在此输入图像描述

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 . 这与matplotlib计算的显示区域(轴限制)有某种关系。

This behaviour is fixed by manually editing the axes range by using set_xlim and set_ylim methods. 通过使用set_xlimset_ylim方法手动编辑轴范围可以set_xlim set_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()

图片

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. 正如评论部分所述,显然Matplotlib已经确定Autoscaling在其官方Github回购中具有基本问题作为发布关键问题 ,将在即将发布的版本中修复。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM