如何使用 Matplotlib 在双对数图上的所有刻度上显示对数间隔的网格线?
我正在尝试绘制一个对数图,该图在您在图的底部和左侧看到的所有刻度上显示对数间隔的网格线。我已经能够使用 matplotlib.pyplot.grid(True)
显示一些网格线,但这仅以 10 次方的间隔显示网格线。举个例子,这是我目前得到的:
我真的很喜欢带有网格线的东西更像这样,网格线的间距并不均匀:
我将如何在 Matplotlib 中实现这一目标?
I'm trying to plot a log-log graph that shows logarithmically spaced grid lines at all of the ticks that you see along the bottom and left hand side of the plot. I've been able to show some gridlines by using matplotlib.pyplot.grid(True)
, but this is only showing grid lines for me at power of 10 intervals. So as an example, here is what I'm currently getting:
I'd really like something with grid lines looking more like this, where the gridlines aren't all evenly spaced:
How would I go about achieving this in Matplotlib?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,您只需在 grid 命令中输入参数
which="both"
,使其变为:其他选项为 'minor' 和 'major',它们是主要刻度(其中显示在您的图表中)以及您缺少的次要刻度。如果您想要实线,那么您也可以使用
ls="-"
作为grid()
的参数。以下是 kicks: 的示例,
它会生成:
有关 Matplotlib 文档
Basically, you just need to put in the parameter
which="both"
in the grid command so that it becomes:Other options for which are 'minor' and 'major' which are the major ticks (which are shown in your graph) and the minor ticks which you are missing. If you want solid lines then you can use
ls="-"
as a parameter togrid()
as well.Here is an example for kicks:
which generates:
More details on the Matplotlib Docs
正如@Bryce所说,在旧版本的matplotlib中,正确的kwarg是which=
majorminor
。我认为颜色较浅的实线可能比虚线更好。请注意,在最新版本的 matplotlib 中,此参数被“both”替换。
As @Bryce says, in older version of matplotlib correct kwarg is which=
majorminor
. I think that solid lines with a lighter color can be better than the dotted lines.Note that in the latest version of matplotlib this argument is replaced by 'both'.