Grid lines in parasitic axes in matplotlib
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"].l开发者_高级运维abel.set_color(p1.get_color())
par.axis["right"].label.set_color(p2.get_color())
host.grid(True)
host.legend()
plt.show()
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()
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:
download the sample matplotlibrc file here or retrieve your copy at site-packages/matplotlib/mpl-data/;
at about line 195 or so of this file, look for a heading (as a comment) "### AXES";
five-six lines below that heading you'll see 'axes-grid'--uncomment this line and set the value to 'True';
now read down this file to about lines 235 or so where you will see the header '### GRIDS';
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.)
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.
精彩评论