views:

99

answers:

1

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

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

alt text

Can anyone suggest how I would go about achieving this in Matplotlib?

+1  A: 

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

Justin Peel
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: http://www.mailinglistarchive.com/html/[email protected]/2010-06/msg00174.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 http://matplotlib.sourceforge.net/api/api_changes.html, but there doesn't seem to be any mention of it...
Bryce Thomas
@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
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
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
@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