Grid Display

Two questions relating to the display of grid lines in a plot:

   1) Can this be controlled programmatically rather than from within
~/.matplotlib/matplotlibrc?

   2) Is there a way to display horizontal grid lines without vertical grid
lines?

   Pointers to the docs where these questions are answered would be adequate.

Rich

···

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Yes. You can do

  from pylab import *
  ...
  axes().grid(True) # For both axes
  axes().xaxis.grid(True) # Just x
  axes().yaxis.grid(True) # Just y

Rather than just an on/off boolean, you can also provide line styles:

  axes().grid(color='r', linestyle='-', linewidth=2)

Cheers,
Mike

Rich Shepard wrote:

···

   Two questions relating to the display of grid lines in a plot:

   1) Can this be controlled programmatically rather than from within
~/.matplotlib/matplotlibrc?

   2) Is there a way to display horizontal grid lines without vertical grid
lines?

   Pointers to the docs where these questions are answered would be adequate.

Rich

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Mike,

   Ah, now I see the syntax for using axes(). However, if I'm embedding the
plots in a wxPython panel, I'm not using pylab. In this environment I also
haven't yet figured out how to add axis labels or specify the range of each
axis. Within pylab on stand-alone test apps it works fine.

Thanks,

Rich

···

On Tue, 27 Nov 2007, Michael Droettboom wrote:

Yes. You can do

  from pylab import *
  ...
  axes().grid(True) # For both axes
  axes().xaxis.grid(True) # Just x
  axes().yaxis.grid(True) # Just y

Rather than just an on/off boolean, you can also provide line styles:

  axes().grid(color='r', linestyle='-', linewidth=2)

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Rich Shepard wrote:

Yes. You can do

  from pylab import *
  ...
  axes().grid(True) # For both axes
  axes().xaxis.grid(True) # Just x
  axes().yaxis.grid(True) # Just y

Rather than just an on/off boolean, you can also provide line styles:

  axes().grid(color='r', linestyle='-', linewidth=2)

Mike,

   Ah, now I see the syntax for using axes(). However, if I'm embedding the
plots in a wxPython panel, I'm not using pylab. In this environment I also
haven't yet figured out how to add axis labels or specify the range of each
axis. Within pylab on stand-alone test apps it works fine.

You can get the axes through the Figure instance. (I don't know how you have your embedding set up, but if it's something like embedding_in_wx.py, there's the line "self.fig = Figure((9, 8), 75)", so self.fig is a Figure instance).

   self.fig.gca() # gets the current axes
   self.fig.axes # is a Python list of axes -- useful if you have more than one and you need to access a particular one of them

So, to do stuff with the grid:

   self.fig.gca().grid(True)

To answer your other questions:

   axes = self.fig.gca()
   axes.set_xlabel("This is the x-axis label")
   axes.set_ylabel("This is the y-axis label")
   axes.set_xlim((-1.25, 1.25))
   axes.set_ylim((-2.0, 2.0))

Cheers,
Mike

···

On Tue, 27 Nov 2007, Michael Droettboom wrote:

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Michael Droettboom wrote:

You can get the axes through the Figure instance. (I don't know how you have your embedding set up, but if it's something like embedding_in_wx.py, there's the line "self.fig = Figure((9, 8), 75)", so self.fig is a Figure instance).

   self.fig.gca() # gets the current axes
   self.fig.axes # is a Python list of axes -- useful if you have more than one and you need to access a particular one of them

Or just keep track of the axes object when it is created. In embedding_in_wx.py:

     def plot_data(self):
         # Use ths line if using a toolbar
         a = self.fig.add_subplot(111)

         ....

"a" is the axes instance.

Eric

   Ah, now I see the syntax for using axes(). However, if I'm embedding the
plots in a wxPython panel, I'm not using pylab. In this environment I also
haven't yet figured out how to add axis labels or specify the range of each
axis. Within pylab on stand-alone test apps it works fine.

Check the class library documentation for the axes() object.
http://matplotlib.sourceforge.net/matplotlib.axes.html

I've also been known to use epydoc to generate my own local version of
the class library (since the sourceforge site is ... tempermental at
best).

To enable gridlines, try something like:

axes.xaxis.grid(b=True,which='major')

and variations with xaxis/yaxis and major/minor.

To add labels, try something like:

axes.set_ylabel(string, fontproperties)

To set ranges, try something like:

axes.set_xlim(range).

Your best bet is really to explore the object set in the class reference docs.

···

Thanks, Mike. As I'm just starting with matplotlib I need to climb the
learning curve as quickly as I can.

Thanks for the pointers,

Rich

···

On Tue, 27 Nov 2007, Michael Droettboom wrote:

You can get the axes through the Figure instance. (I don't know how you have your embedding set up, but if it's something like embedding_in_wx.py, there's the line "self.fig = Figure((9, 8), 75)", so self.fig is a Figure instance).

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Eric,

   Thanks. I had not picked that up.

Rich

···

On Tue, 27 Nov 2007, Eric Firing wrote:

Or just keep track of the axes object when it is created. In
embedding_in_wx.py:

   def plot_data(self):
       # Use ths line if using a toolbar
       a = self.fig.add_subplot(111)

"a" is the axes instance.

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Check the class library documentation for the axes() object.
http://matplotlib.sourceforge.net/matplotlib.axes.html

Anthony,

   I've looked at this but didn't absorb it all. Now I'll spend more time
with it.

Your best bet is really to explore the object set in the class reference docs.

   That I will.

Thanks very much,

Rich

···

On Tue, 27 Nov 2007, Anthony Floyd wrote:

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863