如何使用 Matplotlib 在双对数图上的所有刻度上显示对数间隔的网格线?

发布于 2024-09-16 01:45:21 字数 368 浏览 5 评论 0原文

我正在尝试绘制一个对数图,该图在您在图的底部和左侧看到的所有刻度上显示对数间隔的网格线。我已经能够使用 matplotlib.pyplot.grid(True) 显示一些网格线,但这仅以 10 次方的间隔显示网格线。举个例子,这是我目前得到的:

Alt text

我真的很喜欢带有网格线的东西更像这样,网格线的间距并不均匀:

Alt text

我将如何在 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:

Alt text

I'd really like something with grid lines looking more like this, where the gridlines aren't all evenly spaced:

Alt text

How would I go about achieving this in Matplotlib?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2024-09-23 01:45:21

基本上,您只需在 grid 命令中输入参数 which="both" ,使其变为:

matplotlib.pyplot.grid(True, which="both")

其他选项为 'minor' 和 'major',它们是主要刻度(其中显示在您的图表中)以及您缺少的次要刻度。如果您想要实线,那么您也可以使用 ls="-" 作为 grid() 的参数。

以下是 kicks: 的示例,

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(0, 100, .5)
y = 2 * x**3

plt.loglog(x, y)
plt.grid(True, which="both", ls="-")
plt.show()

它会生成:

a log-log graph

有关 Matplotlib 文档

Basically, you just need to put in the parameter which="both" in the grid command so that it becomes:

matplotlib.pyplot.grid(True, which="both")

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 to grid() as well.

Here is an example for kicks:

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(0, 100, .5)
y = 2 * x**3

plt.loglog(x, y)
plt.grid(True, which="both", ls="-")
plt.show()

which generates:

a log-log graph

More details on the Matplotlib Docs

薄暮涼年 2024-09-23 01:45:21

正如@Bryce所说,在旧版本的matplotlib中,正确的kwarg是which=majorminor。我认为颜色较浅的实线可能比虚线更好。

plt.grid(True, which="majorminor", ls="-", color='0.65')

请注意,在最新版本的 matplotlib 中,此参数被“both”替换。

plt.grid(True, which="both", ls="-", color='0.65')

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.

plt.grid(True, which="majorminor", ls="-", color='0.65')

Note that in the latest version of matplotlib this argument is replaced by 'both'.

plt.grid(True, which="both", ls="-", color='0.65')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文