How do I show logarithmically spaced grid lines at all ticks on a log-log plot using Matplotlib?

 ̄綄美尐妖づ 提交于 2020-02-26 06:30:19

问题


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?


回答1:


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:




回答2:


As @Bryce says, in my machine the correct kwarg is majorminor. I think that solid lines with a lighter color can be better than dotted lines.

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

works for me.



来源:https://stackoverflow.com/questions/3590905/how-do-i-show-logarithmically-spaced-grid-lines-at-all-ticks-on-a-log-log-plot-u

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!