2016-11-03 81 views
1

有沒有人知道如何更新函數scatter的s參數而無需再次將x和y作爲參數?我正在試圖製作一個動畫,其中散點圖的大小增加了,但是我可以讓它工作的唯一方法是每次更新區域時都要給x和y作爲參數。Python:如何在不更新x和y的情況下更新分散區域

這是我的工作代碼:

#population plot 
#plots a graph of the input from a file 

import matplotlib.pyplot as plt; 
import classplace as p; 
import matplotlib.animation as amt 

def animate(i, population, latitude, longitude, colour): 
     scalefactor = 0.00005; 
     area = []; 
     for n in population: 
      area.append(n*scalefactor*i); 

     plt.scatter(longitude, latitude, s = area, c = colour); 
     del area;  
try: 
    readFile = open("GBplaces.csv", "r"); 

except: 
    print("Something went wrong! Can't open the file GBplaces.csv!\n"); 

else: 
    header = False; 
    places = []; 

    #stores the data in a list, where each element of the list is of class place, converting when necessary. 
    #Stores the header in a string 
    for line in readFile: 
     if(line[0] != '%'): 
      words = line.rstrip(); 
      words = words.split(','); 
      places.append(p.Place(words[0], words[1], int(words[2]), float(words[3]), float(words[4]))); 

    #closes readFile 
    readFile.close(); 

    #creates an array of colours where cities are green and towns are yellow 
    #creates an array of longitude 
    #creates an array of latitude 
    #creates an array of population 

    colour = []; 
    longitude = []; 
    latitude = []; 
    population = []; 

    for n in range(p.Place.numcities + p.Place.numtowns): 
     if(places[n].tipe == "City"): colour.append("g"); 
     else: colour.append("y"); 

     longitude.append(places[n].longitude); 
     latitude.append(places[n].latitude); 
     population.append(places[n].population); 

    fig = plt.figure(); 

    ani = amt.FuncAnimation(fig, animate, frames=50, fargs = (population, latitude, longitude, colour), interval=0.1, repeat = False, blit = False) 
    plt.show() 

哪裏classplace與類定義

#classplace.py 
#place class definition 

class Place: 
    numcities = 0; 
    numtowns = 0; 

    def __init__(self, name, tipe, population, latitude, longitude): 
     self.name = name; 
     self.tipe = tipe; 
     self.population = population; 
     self.latitude = latitude; 
     self.longitude = longitude; 

     if(self.tipe == "City"): Place.numcities += 1; 
     elif(self.tipe == "Town"): Place.numtowns += 1; 
     else: 
      print("Instance is not allowed. You need to specify if %s is a City or Town.\n" %(self.name)); 
      del self; 

一個文件,如果我可以只更新區域的效率會得到這樣更好。 謝謝你的幫助!

回答

0

scatter返回matplotlib.collections.PathCollection對象。您可以使用其方法get_sizes()set_sizes()來檢索或設置點的大小。這是你的例子的修改。

import matplotlib.pyplot as plt; 
import matplotlib.animation as amt 

# to embed animation as html5 video in Jupyter notebook 
from matplotlib import rc 
rc('animation', html='html5') 

def animate(i, population, latitude, longitude, colour): 
    scalefactor = 0.001 
    area = [n*scalefactor*i for n in population] 
    # it is better to use list comprehensions here 

    sc.set_sizes(area) 
    # here is the answer to the question 

    return (sc,) 

#creates an array of colours where cities are green and towns are yellow 
#creates an array of longitude 
#creates an array of latitude 
#creates an array of population 

# pick some random values 

colour = ['g', 'g', 'y', 'y', 'y']; 
longitude = [1, 2, 3, 4, 5]; 
latitude = [1, 3, 2, 5, 4]; 
population = [1000, 2000, 5000, 4000, 3000]; 

fig = plt.figure() 
sc = plt.scatter(longitude, latitude, s=population) 

# I didn't managed this to work without `init` function 

def init(): 
    return (sc,) 

ani = amt.FuncAnimation(fig, animate, frames=200, init_func=init, 
         fargs=(population, latitude, longitude, colour), 
         interval=10, repeat = False, blit = True) 

animation

相關問題