相关文章推荐
买醉的蛋挞  ·  c# - SimpleHttpServer ...·  1 年前    · 
销魂的小熊猫  ·  Javascript ...·  1 年前    · 
刚分手的咖啡豆  ·  go - Often restarting ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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

I've found that on my machine using "both" results in neither major or minor grid lines being shown. With some googling, I found this post: mailinglistarchive.com/html/… which seems to suggest that older versions of matplotlib require using "majorminor" instead of "both". Do you know whether there is any official documentation of this change between versions? I've looked at matplotlib.sourceforge.net/api/api_changes.html, but there doesn't seem to be any mention of it... – Bryce Thomas Aug 28, 2010 at 17:28 @Bryce I have no idea when that change went into effect. I generally just use the newest version. I'm actually not all that proficient with matplotlib. – Justin Peel Aug 28, 2010 at 17:30 looking at one of those messages makes me think that it happened on June 9 of 2010. I'm not sure what version that would be, but it was quite recent. – Justin Peel Aug 28, 2010 at 18:37 Minor grid lines make a figure look very crowded. Alternatively, you could just try increasing your major-minor x/y tick sizes your in matplotlibrc file. – Gökhan Sever Aug 28, 2010 at 18:50 @Gokan I agree to some extent, though I've found if you use dotted rather than solid lines the grid is less invasive. – Bryce Thomas Aug 29, 2010 at 5:20

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')
                I needed to set zorder=3 to my actual (histogram) plot, because the grid lines would otherwise strike through my bars.
– moooeeeep
                Mar 28, 2019 at 16:47
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.