0

I will plot hundreds of dots and recalculate the data using a slider. I want to load the sizes of each individual dot from the array sizes, but plt.plot does not allow for a markersize array, they all must be the same.

The data set is large so I don't want to loop through each point and plot it separately, so I thought I would use bob = plt.scatter(x, y, s=sizes).

I think I can use bob.set_sizes(sizes) to update the sizes, but now I can't figure out how to update x and y.

Question: How can I update the x and y data arrays in a matplotlib scatter plot without implementing animation?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)

x = np.linspace(0, 1, 10)
y = np.cos(10*x)
sizes  = 10*(x + y**2)

l, = plt.plot(x, y, 'ok') #s=sizes

plt.axis([-0.1, 1.1, -1.1, 1.1])

axcolor = 'lightgoldenrodyellow'

ax_slider  = plt.axes([0.25, 0.10, 0.65, 0.03], facecolor=axcolor)
N_slider   = Slider(ax_slider, 'N', 5,  50, valinit=10,   valstep=1)

def update(val):
    N = int(N_slider.val)
    x = np.linspace(0, 1, N)
    y = np.cos(10*x)
    sizes  = 10*(x + y**2)
    l.set_xdata(x)
    l.set_ydata(y)
    # l.set_s(sizes) no such thing
    fig.canvas.draw_idle()

N_slider.on_changed(update)

plt.show()

enter image description here

uhoh
  • 3,713
  • 6
  • 42
  • 95
  • Also there is [an example](https://matplotlib.org/gallery/animation/rain.html#) in the matplotlib documentation. – ImportanceOfBeingErnest Jun 29 '19 at 13:06
  • @ImportanceOfBeingErnest those answers don't cover sliders. I don't want to get involved in an animation. Are you sure there is no way to update the scatter plot data without resorting to an animation? – uhoh Jun 29 '19 at 13:06
  • There is no essential difference between the updating function called by a Slider and the one called by an animation, right? So why would this not be answered by any of the two duplicates? – ImportanceOfBeingErnest Jun 29 '19 at 13:08
  • @ImportanceOfBeingErnest what does "right?" mean? If that's true, then please assert it rather than asking me! But in the mean time I ask again are you sure there is no way to update the scatter plot data without resorting to an animation? Because that's the question that I have asked here. – uhoh Jun 29 '19 at 13:09
  • You are asking how to change the properties of `bob`, which, in the other answers is called `scat` or `sc`. You do by no means need a `FuncAnimation`. – ImportanceOfBeingErnest Jun 29 '19 at 13:13
  • @ImportanceOfBeingErnest I see `bob.set_offsets()` but I don't see how that can be used to change both the x and the y data. And with the instaclose, nobody will be able to post an answer telling me how. – uhoh Jun 29 '19 at 13:20
  • You are trying very hard not to spend time on understanding the answers. In the duplicate x and y *are* changed, so it's directly applicable. There is no need for the same answer here again, and yes, duplicates are meant to prevent duplicate answers. – ImportanceOfBeingErnest Jun 29 '19 at 13:23
  • @ImportanceOfBeingErnest okay, `bob.set_offsets(xy.T)` where `xy.T` is Nx2 does indeed work. I saw the animation-based answers and got scared. Thanks! – uhoh Jun 29 '19 at 13:29
  • ```set_offsets[new_x, new_y]``` doesnt seem to work for my code. It just returns a blank canvas on my end. I am not sure what I am doing wrong. – w41g87 Aug 10 '23 at 23:59
  • @w41g87 I can't help much based on so little information, you can consider asking a new question. However try some standard steps - find the minimal script that reproduces your problem, double check that there's nothing wrong with your new_x, new_y (are they out of range of the limits of the axes - I don't think set_offsets will change the axes limits, they'll remain fixed) are there any other problems with them? – uhoh Aug 11 '23 at 00:17
  • @w41g87 Once you have your [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) with valid new_x, new_y post the full script a new question. You are welcome to ping me here with a link to it and I'll take a look as soon as I can. – uhoh Aug 11 '23 at 00:18

0 Answers0