helloswift123
helloswift123

Reputation: 163

How to animate and update the size of scatter markers

I have two data sets, Points and Pointsize. I want to animate the plot with the change of coordinate(Points) and size(Pointsize) of points. But I only can update Points. The code below is showing that three points move with change of data of Points. What I want is, the points not only move, but also change their sizes.I tried to use scat.set_offsets(Points['xy'],Pointsize) to achieve my goal. But the error shows "TypeError: set_offsets() takes exactly 2 arguments (3 given)". I also tried to use duplicate set_offsets to update both Points['xy'] and Pointsize separately. The error shows "ValueError: total size of new array must be unchanged".

I have no idea how to solve that problem. Someone can tell me a way or solution to achieve my goal? I will appreciate your help. Thank you very much.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

def updata(frame_number):
    current_index = frame_number % 3
    a = [[10,20,30],[40,50,60],[70,80,90]]
    Points['xy'][:,0] = np.asarray(a[current_index])
    Points['xy'][:,1] = np.asarray(a[current_index])
    Pointsize = a[current_index]
    scat.set_offsets(Points['xy'])
    #scat.set_offsets(Pointsize)
    #scat.set_offsets(Points['xy'],Pointsize)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title("For Dataset %d" % current_index)


fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111)
Points = np.zeros(3,dtype=[('xy',float,2)])
Pointsize = [10] * 3
scat = ax.scatter(Points['xy'][:,0],Points['xy'][:,1],s=Pointsize,alpha=0.3,edgecolors='none')
ax.set_xlim(0,100)
ax.set_ylim(0,100)
animation = FuncAnimation(fig,updata,frames=50,interval=600)
plt.show()

Upvotes: 0

Views: 1762

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339120

As seen in rain simulation example, use .set_sizes

scat.set_sizes(Pointsize)

to update the size of scatter points

Upvotes: 3

Related Questions