1

This is probably a very naive question, but I can't find clear instructions how to do this. What I am trying to do in matplotlib is:

  1. Do some calculations
  2. Create a figure or an axis object?
  3. Add data to the object, plot it
  4. Do some more calculations
  5. Add new data to the object as a new layer (not extend existing data, but have a new line/scatter)

I want to do something like:

x = rand(10)
y = rand(10)
p = plot(x,y, '.', label="x:y")

# other code, possibly a new cell in the notebook

a = rand(10)
b = rand(10)

p.plot(a,b, '--', label="a:b") # error. what do I do to add a layer here?

The final plot should contain both (x,y) and (a,b) data.

I am NOT interested in having a bunch of subplots (every example seems to have those) and I want to do the above in the pylab plot (in an IPython notebook).

Thanks!

2 Answers 2

1

Your problem is that what plot returns isn't the actual object where things are being plotted, but that's what you're trying to use it as. So you need to define p to actually be the object you want to plot things in.

import pylab as py
from numpy.random import rand

x = rand(10)
y = rand(10)
p = py.gca()
p.plot(x,y, '.', label="x:y")

# other code, possibly a new cell in the notebook

a = rand(10)
b = rand(10)

p.plot(a,b, '--', label="a:b")

In fact in your original code, if you just removed the p. in your second plot command, you'd get what I think you were after.

Sign up to request clarification or add additional context in comments.

10 Comments

To make this better, I would a) avoid pylab. Promulgating the use of pylab in scripting is not not not good (see here), and b) describe what the object you mention actually is (i.e., an Axes object)
Another question: how do I plot p from the above example elsewhere in the code? Is there a p.show() or something like that?
@ajean Can you recommend an alternative for the purposes of plotting things? Your link doesn't seem to be saying don't use pylab, but rather don't import it in a certain way. I'm not an expert, so I may not be interpreting it correctly.
@AlexanderFlyax There will be a way to do that, but not through p.show(). p is an axes object sitting in a figure somewhere. So you'll have to get ahold of the figure itself and show that figure. (note, you won't be able to take those axes and put them into other figures easily). This is at the edge of my comfort zone, so I'm going to pass on trying to answer.
@Joel, keep in mind that pylab != matplotlib. Pylab includes matplotlib, but it also does tons of other funny business behind the scenes. If you must, use matplotlib's pyplot interface (also != pylab). You should learn what the differences are between those three things, it will make your life easier in the long run!
|
0

One way that I have solved this issue is to create your figure and axis as follows:

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3],[1,2,3])

Set figure parameters as needed here and finally call show but with block=False

plt.show(block=False)

Then from other functions you can add to the same axis by using Matplotlibs plt.gca() function that returns the current or last clicked on axis by the user as follows (but dont create a new figure or axis) and using Matplotlibs draw() function:

plt.gca().plot([4,5,6],[4,5,6])
plt.draw()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.