如何使用 Matplotlib 在对数图上的所有刻度上显示对数间隔的网格线? [英] How do I show logarithmically spaced grid lines at all ticks on a log-log plot using Matplotlib?

查看:33
本文介绍了如何使用 Matplotlib 在对数图上的所有刻度上显示对数间隔的网格线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我真的很喜欢网格线看起来更像这样的东西,其中网格线不是均匀分布的:

我将如何在 Matplotlib 中实现这一目标?

解决方案

基本上只需要在grid命令中放入参数which="both"这样就变成了:

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

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

这是一个踢球的例子:

将 numpy 导入为 np从 matplotlib 导入 pyplot 作为 pltx = np.arange(0, 100, .5)y = 2 * x**3plt.loglog(x, y)plt.grid(True, which="both", ls="-")plt.show()

生成:

有关 Matplotlib 文档的更多详细信息p>

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?

解决方案

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:

More details on the Matplotlib Docs

这篇关于如何使用 Matplotlib 在对数图上的所有刻度上显示对数间隔的网格线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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