views:

467

answers:

2

Can you draw the grid lines in a plot with parasitic axes in matplotlib?

I try this, based on the samples for grids and for parasitic axes, but grid drawing is not performed:

from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
import matplotlib.pyplot as plt

fig = plt.figure(1)

host = SubplotHost(fig, 111)
fig.add_subplot(host)

par = host.twinx()

host.set_xlabel("Distance")
host.set_ylabel("Density")
par.set_ylabel("Temperature")

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par.plot([0, 1, 2], [0, 3, 2], label="Temperature")

host.axis["left"].label.set_color(p1.get_color())
par.axis["right"].label.set_color(p2.get_color())

host.grid(True)

host.legend()

plt.show()
+2  A: 

For plots of this type, it's not always easy to tell at a glance which object should make the call to 'grid'.

One way around this inconvenience--i.e., to get the grid lines on your plot without having to change any of your code and worry about whether you have the right object calling 'grid'--is to edit your config *file*. That might be enough for you to do exactly what you need to do, but just in case:

  1. download the sample matplotlibrc file here or retrieve your copy at site-packages/matplotlib/mpl-data/;

  2. at about line 195 or so of this file, look for a heading (as a comment) "### AXES";

  3. five-six lines below that heading you'll see 'axes-grid'--uncomment this line and set the value to 'True';

  4. now read down this file to about lines 235 or so where you will see the header '### GRIDS';

  5. uncomment the next three lines ('grid.color', 'grid.linestyle', and grid.linewidth') and supply reasonable values for those three parameters (mine are: 'darkslategray', ':', and 0.7, respectively). (The ':' value means my grid lines will be dotted lines.)

  6. save that file as: ~/.matplotlibrc/matplotlibrc (in other words, create a directory in your top level user directory called '.matplotlibrc', don't forget the leading '.', then name this file you've been editing 'matplotlibrc'.

That's it. (The downside is when you are creating plots for which you do not want grid lines--for those, i would keep this config file as is and create additional config files as needed and switch among them (see the relevant Matplotlib page for how to do that.)

Also, this config file is easy to edit using ipython--that's probably how most users do it, but that might have been confusing here.

doug
@doug, I just tried this for this plot type and the grid still does not show. Also, you can adjust these options on a script by script basis by using the matplotlib.rcParams dict (http://matplotlib.sourceforge.net/users/customizing.html), might be easier than maintaining separate config files.
Mark
i'm aware of that--wanted to keep my example straightforward and limited to addressing your question. If you are not seeing a grid, then you haven't configured matplotlib correctly. When it is configured correctly, it does work--i pasted your code into my editor and ran it and i got a plot w/ a grid, as i expected.
doug
Sorry for not following up, guys. I have family health problems, and I will try out your solutions as soon as I have my laptop in the same city as me.
Pablo Rodriguez
The grid doesn't show up for me, either.
Pablo Rodriguez
+3  A: 

From the discussion here it looks like this is a bug in the .99 release.

(I'm not sure why it works for doug but no combination of rcParams works for me on version 0.99.1.1-r1.)

From that link the answer is to make a call to:

host.toggle_axisline(False)

What the toggle_axisline does is simply to make the xaxis and yaxis (which are responsible for drawing ticks, ticklabels, etc in the mainline mpl) visible again, and make axis["bottom"] and etc invisible.

The whole program becomes:

from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
import matplotlib.pyplot as plt

fig = plt.figure(1)

host = SubplotHost(fig, 111)
fig.add_subplot(host)

par = host.twinx()

host.set_xlabel("Distance")
host.set_ylabel("Density")
par.set_ylabel("Temperature")

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par.plot([0, 1, 2], [0, 3, 2], label="Temperature")

host.axis["left"].label.set_color(p1.get_color())
par.axis["right"].label.set_color(p2.get_color())

host.toggle_axisline(False)
host.grid(True)

host.legend()

plt.show()

alt text

Mark
Great! Axis label colour is lost, however. It's not vital and I can wait until the 1.0 release.
Pablo Rodriguez
oh thanks! you saved me, I was about to go back to generating the axes manually
Longpoke