Removing a line from a plot

Hi Gang, Thank you guys, and thanks for the Wiki entry :wink:

    > Andrea.

    > I have a further small question. When I add a line to my
    > plot, the axes gracefully rescale to accomodate the new
    > data plotted. However, when I remove a line, they do not
    > rescale, even if I call:

    > locator = self.leftaxis.yaxis.get_major_locator()
    > locator.autoscale()

    > Or:

    > self.leftaxis.autoscale_view(scalex=False, scaley=True)

    > I also call self.canvas.draw(), self.Refresh()... nothing
    > happens. Is there a way to make the axes rescale after
    > removing a line from a plot?

The Axes instance has two BBox (bounding box) instances -- the dataLim
and the viewLim. The dataLim store a rectangle that bounds all the
data in the Axes, and the viewLim are the x and y view limits, ie,
xlim and ylim. Autoscaling sets the viewLim based on the dataLim.

When you add lines to the plot, the dataLim are updated, but when you
remove data with del ax.lines[-1] etc, the dataLim are not updated.
If all you have in the Axes are line instances, you can update the
dataLim with the remaining lines, but first you must tell it to ignore
it's current limits. You do this with the ignore flag

# after removing a line, do
ignore = True
for line in ax.lines:
    x = line.get_xdata()
    y = line.get_ydata()
    ax.dataLim.update_numerix(x, y, ignore)
    ignore = False

If you have other data in your Axes, eg Polygons or Collections, it is
a bit more complicated. It would be useful to have an Axes method
like "auto_datalim" to for the datalim to readjust to all the current
data.

After you have tested this, would you mind updating the wiki with this
information?

Thanks,
JDH

Hi John,

    sorry to come back so late with this subject. Well, I have tried
your suggestion:

When you add lines to the plot, the dataLim are updated, but when you
remove data with del ax.lines[-1] etc, the dataLim are not updated.
If all you have in the Axes are line instances, you can update the
dataLim with the remaining lines, but first you must tell it to ignore
it's current limits. You do this with the ignore flag

# after removing a line, do
ignore = True
for line in ax.lines:
   x = line.get_xdata()
   y = line.get_ydata()
   ax.dataLim.update_numerix(x, y, ignore)
   ignore = False

No way, it doesn't update the axes. Uhm, in my plot I have only lines
and one legend, nothing more. Even if I try a simple case with 2 lines
and I remove one of them, the axes still stay with their previous
limits. I must be missing something.

After you have tested this, would you mind updating the wiki with this
information?

No problem, as soon as I am able to update the axes, I will add that
info to the wiki.

Thank you.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/

Anyone find an answer to this one? I’m running into this issue with 0.90.1 and I found this in the archives. I tried the suggested code and it doesn’t seem to work for me. Right now my workaround is

    min_x = min([min(line.get_xdata()) for line in self.axes.lines])
    max_x = max([max(line.get_xdata()) for line in self.axes.lines])           
    self.axes.set_xlim(min_x,max_x)

Any suggestions?

Thanks,
Greg

···

On Tue, Nov 14, 2006 at 8:13 PM, Andrea Gavana <andrea.gavana@…287…> wrote:

Hi John,

sorry to come back so late with this subject. Well, I have tried

your suggestion:

When you add lines to the plot, the dataLim are updated, but when you

remove data with del ax.lines[-1] etc, the dataLim are not updated.

If all you have in the Axes are line instances, you can update the

dataLim with the remaining lines, but first you must tell it to ignore

it’s current limits. You do this with the ignore flag

after removing a line, do

ignore = True

for line in ax.lines:

x = line.get_xdata()

y = line.get_ydata()

ax.dataLim.update_numerix(x, y, ignore)

ignore = False

No way, it doesn’t update the axes. Uhm, in my plot I have only lines

and one legend, nothing more. Even if I try a simple case with 2 lines

and I remove one of them, the axes still stay with their previous

limits. I must be missing something.

After you have tested this, would you mind updating the wiki with this

information?

No problem, as soon as I am able to update the axes, I will add that

info to the wiki.

Thank you.

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”

http://xoomer.virgilio.it/infinity77/


Take Surveys. Earn Cash. Influence the Future of IT

Join SourceForge.net’s Techsay panel and you’ll get the chance to share your

opinions on IT & business topics through brief surveys - and earn cash

http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users