1
xとyを引数としてもう一度与えなくても、関数scatterのs引数を更新する方法を知っている人はいますか?分散円のサイズが大きくなるアニメーションを作ろうとしていますが、私がそれを働かせる唯一の方法は、私が領域を更新するたびにxとyを引数として与えることです。Python:xとyを更新せずにスキャターで領域を更新する方法
これは私が働いているコードです:
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;
を持つファイルである
#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()
。 ありがとうございます!