Removing a data series

I
did my best to search the examples, mailing list, etc. but was surprised that I
didn’t even find this question asked, let alone answered.

My
basic question: Is it possible to remove a data series from a matplotlib plot?

Longer
question:

I’m
using matplotlib to generate graph images for an interactive web site. Through
the web site, the user can create a plot and add data series to it. I’d also
like the user to be able to remove a data series. At the moment, I am
accomplishing this by clearing the plot and replotting all the data series
except of course for the one to be removed. This seems inefficient.

So
something like:

  fig

= Figure()

  ax

= fig.add_subplot(111)

  series1

= ax.plot()

  series2

= ax.plot()

  series3

= ax.plot()

And
now I want to remove series2 from the plot.

Is
this possible? If it is, can anyone point me to the relevant part of the
user’s guide/class library? Or is the best approach to clear the plot and
replot 1 and 3 (and reset all the axes, legends, etc.)

I
apologize if this is a FAQ, but as I said, my search for it came up empty.

Thanks,

-Dan

···

karipid@…1338…

Hi Dan,

I did my best to search the examples, mailing list, etc. but was surprised
that I didn't even find this question asked, let alone answered.

Coincidentally it was asked just yesterday by Andrea Gavana, but you
may have missed it in your search (it may have crossed with your
posting time).

My basic question: Is it possible to remove a data series from a matplotlib
plot?

Here is a little self-contained example you can try (N==numpy, P=pylab):

In [24]: x = N.arange(10)

In [25]: fig = P.figure()

In [26]: ax = fig.add_subplot(111)

In [27]: ax.plot(x)
Out[27]: [<matplotlib.lines.Line2D instance at 0x427ce7ec>]

In [28]: ax.plot(x+10)
Out[28]: [<matplotlib.lines.Line2D instance at 0x427ce88c>]

In [29]: ax.plot(x+20)
Out[29]: [<matplotlib.lines.Line2D instance at 0x427ce9ac>]

In [30]: P.show()

In [31]: ax.lines
Out[31]:
[<matplotlib.lines.Line2D instance at 0x427ce7ec>,
<matplotlib.lines.Line2D instance at 0x427ce88c>,
<matplotlib.lines.Line2D instance at 0x427ce9ac>]

In [32]: del ax.lines[1]

In [33]: P.show()

This makes the middle line disappear from the plot.

Regards,

f

···

On 11/9/06, Dan Karipides <karipid@...1338...> wrote:

My basic question: Is it possible to remove a data series from a matplotlib
plot?

A quite similar question was asked yesterday:
The lines are stored in "ax.lines", by chronological order. If you want to
delete the second one, just use del(ax.lines[1]), and redraw if needed.

Fernando,

Thanks, this is exactly what I need.

BTW, I think I missed it in the search because the archive here:

http://sourceforge.net/mailarchive/forum.php?forum_id=33405

doesn't show anything more recent that 10/02/2006. I'm not sure why this
is.

Thanks again,

-Dan

···

-----
karipid@...1338...

-----Original Message-----
From: Fernando Perez [mailto:fperez.net@…287…]
Sent: Thursday, November 09, 2006 9:46 AM
To: Dan Karipides
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Removing a data series

Hi Dan,

On 11/9/06, Dan Karipides <karipid@...1338...> wrote:

I did my best to search the examples, mailing list, etc. but was surprised
that I didn't even find this question asked, let alone answered.

Coincidentally it was asked just yesterday by Andrea Gavana, but you
may have missed it in your search (it may have crossed with your
posting time).

My basic question: Is it possible to remove a data series from a

matplotlib

plot?

Here is a little self-contained example you can try (N==numpy, P=pylab):

In [24]: x = N.arange(10)

In [25]: fig = P.figure()

In [26]: ax = fig.add_subplot(111)

In [27]: ax.plot(x)
Out[27]: [<matplotlib.lines.Line2D instance at 0x427ce7ec>]

In [28]: ax.plot(x+10)
Out[28]: [<matplotlib.lines.Line2D instance at 0x427ce88c>]

In [29]: ax.plot(x+20)
Out[29]: [<matplotlib.lines.Line2D instance at 0x427ce9ac>]

In [30]: P.show()

In [31]: ax.lines
Out[31]:
[<matplotlib.lines.Line2D instance at 0x427ce7ec>,
<matplotlib.lines.Line2D instance at 0x427ce88c>,
<matplotlib.lines.Line2D instance at 0x427ce9ac>]

In [32]: del ax.lines[1]

In [33]: P.show()

This makes the middle line disappear from the plot.

Regards,

f

Since this is the third time I've seen this question pop up in a few
months, I've put an entry in the wiki for it here
(http://www.scipy.org/Cookbook/Matplotlib/DeletingAnExistingDataSeries).
Fernando, I hope you don't mind me using your example on the page - it
seemed the most thorough.

A.

···

On 10/11/06, Dan Karipides <karipid@...1338...> wrote:

Fernando,

Thanks, this is exactly what I need.

BTW, I think I missed it in the search because the archive here:

http://sourceforge.net/mailarchive/forum.php?forum_id=33405

doesn't show anything more recent that 10/02/2006. I'm not sure why this
is.

Thanks again,

-Dan
-----
karipid@...1338...

--
AJC McMorland, PhD Student
Physiology, University of Auckland

How dare you! I'm going to sue for copyright theft! :wink:

Thanks for the wiki entry, it's certainly a common need.

Regards,

f

···

On 11/9/06, Angus McMorland <amcmorl@...287...> wrote:

Since this is the third time I've seen this question pop up in a few
months, I've put an entry in the wiki for it here
(http://www.scipy.org/Cookbook/Matplotlib/DeletingAnExistingDataSeries).
Fernando, I hope you don't mind me using your example on the page - it
seemed the most thorough.