1) (Simple) Is there a defined behavior for matplotlib
> when it attempts to graph data containing NaN values?
> (OK, I admit-- it's really, really late, and I have tried
> it to see what happens. But even that wouldn't tell me
> if that was the _defined_ behavior :-))
No, it's not defined. I don't know that NaN is defined across
platforms in python. See my recent question on comp.lang.python
Perhaps Todd or Perry can comment on what the status of NaN vis-a-vis
Numeric and numarray. This has come up a number of times before, and
would be nice to be able to handle it. As always, these ease of use
features imply a performance cost that the typical user may not want
to pay....
> 2) I expect to be using matplotlib quite a bit in the
> future, and will likely be using it in a more
> object-oriented mode (i.e. not through the matlab-style
> interface), since that's what I like better. I haven't
> seen any "howto" documents for using the OO API. Do any
> exist? I'd be happy to make notes and write a tutorial as
> I learn, but don't want to duplicate work that already
> exists.
I wrote a little example which I'll include below. The pure OO API is
not really designed to be too user friendly. Eg to instantiate a
line, you do
vline = Line2D(
dpi, bbox,
xdata=x, ydata=y,
color=color,
antialiased=False, # no need to antialias vert lines
transx = ax.xaxis.transData,
transy = ax.yaxis.transData)
It's useful if you want to have total control of the lines
transformations, bounding boxes and so on, but is overkill for making
most plots. Likewise, instantiating your own Axes and Figures
requires a extra overhead. This is addressed more in the example
below, which recommends a hybrid approach.
If you want to take some of this and extend it into a guide of sorts,
that would be great. If what you are looking for is a developer's
guide which describes the process of Figure, Axes, Line2D, Text, etc,
creation and how to use them together, that doesn't exist yet.
However, matplotlib is undergoing rapid development and changes. One
nice thing about having people use the matlab interface is that it
frees me to refactor the OO API. There have been several major
refactorings to date. If a lot of people are using this API, and it's
documented, it is more difficult to change. I am not totally happy
with the current design (eg it us cumbersome to have to pass all those
objects just to create a line) so for now I prefer not to have too
many people creating lots of code with the OO API. I think the
overall design is stable (Figures contain Axes which contain Text,
Axis and Lines, etc) but some of the constructor signatures may
change.
The hybrid approach I recommend below keeps you safely at the
interface level and insulated from any API changes, which can be
painful for application developers. It does, however, enable you to
write more pythonic code.
Here is the new examples/pythonic_matplotlib.py:
"""
Some people prefer to use the python object oriented face rather than
the matlab interface to matplotlib. This example show you how.
Unless you are an application developer, I recommend using part of the
matlab interface, particularly the figure, close, subplot, axes, and
show commands. These hide a lot of complexity from you that you don't
need to see in normal figure creation, like instantiating DPI
instances, managing the bounding boxes of the figure elements,
creating and reaslizing GUI windows and embedding figures in them.
If you are an application developer and want to embed matplotlib in
your application, follow the lead of examples/embedding_in_wx.py,
examples/embedding_in_gtk.py or examples/embedding_in_tk.py. In this
case you will want to control the creation of all your figures,
embedding them in application windows, etc.
If you seen an example in the examples dir written in matlab
interface, and you want to emulate that using the true python method
calls, there is an easy mapping. Many of those examples use 'set' to
control figure properties. Here's how to map those commands onto
instance methods
The syntax of set is
set(object or sequence, somestring, attribute)
if called with an object, set calls
object.set_somestring(attribute)
if called with a sequence, set does
for object in sequence:
object.set_somestring(attribute)
So for your example, if a is your axes object, you can do
a.set_xticklabels()
a.set_yticklabels()
a.set_xticks()
a.set_yticks()
"""
from matplotlib.matlab import figure, close, axes, subplot, show
from matplotlib.numerix import arange, sin, pi
t = arange(0.0, 1.0, 0.01)
fig = figure(1)
ax1 = subplot(211)
ax1.plot(t, sin(2*pi*t))
ax1.grid(True)
ax1.set_ylim( (-2,2) )
ax1.set_ylabel('1 Hz')
ax1.set_title('A sine wave or two')
for label in ax1.get_xticklabels():
label.set_color('r')
ax2 = subplot(212)
ax2.plot(t, sin(2*2*pi*t))
ax2.grid(True)
ax2.set_ylim( (-2,2) )
l = ax2.set_xlabel('Hi mom')
l.set_color('g')
l.set_fontsize(15)
show()