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()