Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear figure before plotting net data #2445

Open
RufinoN opened this issue Jan 18, 2023 · 4 comments
Open

clear figure before plotting net data #2445

RufinoN opened this issue Jan 18, 2023 · 4 comments

Comments

@RufinoN
Copy link

RufinoN commented Jan 18, 2023

Hello,
I'm trying to clear a plot before plotting new data without having to re-create the whole figure again. is there any option for such case?

Here's what I'm doing

    from vispy import plot
    import vispy
    vispy.use('pyqt5')
    
    fig = plot.Fig(size=(400,400), show=False)
    
    # gerenate firt data
    x = np.linspace(0, 10, 100)
    y = np.sin(x)
    data = np.c_[x, y]

    fig[0,0].plot(data)
    img=fig.render()
    
    # generate a new data 
    new_x = np.linspace(0, 5, 100)
    new_y = np.cos(new_x)
    new_data = np.c_[new_x, new_y]
    fig[0,0].plot(new_data)
    
    # Render the Vispy plot to an image
    img=fig.render()

the problem is that when i plot the second data. it also carry the data from the first plot.
one way to solve this it to create a new fig, but the process is way slower than plotting a new data. so, I'm wondering if there's a way to clear the previous plot and then plot new data.

@djhoese
Copy link
Member

djhoese commented Jan 18, 2023

fig[0,0].set_data(data)

Does calling set_data before an explicit .plot call actually work? I guess I'm surprised and I'm not sure what types of widgets it is creating. I've been out sick for the past week or so and won't have much time to dive deep into this, but wanted to let you know someone at least saw it.

@djhoese
Copy link
Member

djhoese commented Jan 18, 2023

Could you check your code? fig[0, 0] should be returning a PlotWidget object which (I don't think) doesn't have a set_data method on it.

@RufinoN
Copy link
Author

RufinoN commented Jan 18, 2023

Could you check your code? fig[0, 0] should be returning a PlotWidget object which (I don't think) doesn't have a set_data method on it.

you're right. it was a mistake. I was trying something before copying the code so forgot to set it back to plot. I'll fix it. but still have to clear the data before plotting a new one

@djhoese
Copy link
Member

djhoese commented Jan 18, 2023

Sorry, I should have asked for this earlier, but do you have an image of what this produces? My best guess right now of what is happening is that when you call .plot you are creating a new LinePlot Visual that is being added to the overall Canvas rather than replacing the old one (I think this is inline with what matplotlib does?).

self._configure_2d()
line = scene.LinePlot(data, connect=connect, color=color,
symbol=symbol, line_kind=line_kind,
width=width, marker_size=marker_size,
edge_color=edge_color,
face_color=face_color,
edge_width=edge_width)
self.view.add(line)
self.view.camera.set_range()
self.visuals.append(line)
if title is not None:
self.title.text = title
if xlabel is not None:
self.xlabel.text = xlabel
if ylabel is not None:
self.ylabel.text = ylabel
return line

The key thing to replacing data on the line rather than adding new information is to use that LinePlot visual and call set_data on it. So if you change your code to do:

line_plot = fig[0,0].plot(data)
...
# then later:
line_plot.set_data(...)

That might work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants