Python 如何使用Matplotlib为预计的世界人口增长设置动画?
我画了一幅世界人口的预测图,这是我数天来一直在努力制作的动画。 图为:Python 如何使用Matplotlib为预计的世界人口增长设置动画?,python,matplotlib,animation,Python,Matplotlib,Animation,我画了一幅世界人口的预测图,这是我数天来一直在努力制作的动画。 图为: import numpy as np import pandas as pd import seaborn as sns import matplotlib import matplotlib.pyplot as plt import matplotlib.ticker as ticker from celluloid import Camera dataDict = {'Year': {0: 1950, 1: 195
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from celluloid import Camera
dataDict = {'Year': {0: 1950,
1: 1951,
2: 1952,
3: 1953,
4: 1954,
5: 1955,
6: 1956,
7: 1957,
8: 1958,
9: 1959,
10: 1960,
11: 1961,
12: 1962,
13: 1963,
14: 1964,
15: 1965,
16: 1966,
17: 1967,
18: 1968,
19: 1969,
20: 1970,
21: 1971,
22: 1972,
23: 1973,
24: 1974,
25: 1975,
26: 1976,
27: 1977,
28: 1978,
29: 1979,
30: 1980,
31: 1981,
32: 1982,
33: 1983,
34: 1984,
35: 1985,
36: 1986,
37: 1987,
38: 1988,
39: 1989,
40: 1990,
41: 1991,
42: 1992,
43: 1993,
44: 1994,
45: 1995,
46: 1996,
47: 1997,
48: 1998,
49: 1999,
50: 2000,
51: 2001,
52: 2002,
53: 2003,
54: 2004,
55: 2005,
56: 2006,
57: 2007,
58: 2008,
59: 2009,
60: 2010,
61: 2011,
62: 2012,
63: 2013,
64: 2014,
65: 2015,
66: 2016,
67: 2017,
68: 2018,
69: 2019,
70: 2020,
71: 2091,
72: 2092,
73: 2093,
74: 2094,
75: 2095,
76: 2096,
77: 2097,
78: 2098,
79: 2099,
80: 2100},
'billion': {0: 2.5,
1: 2.6,
2: 2.6,
3: 2.7,
4: 2.7,
5: 2.8,
6: 2.8,
7: 2.9,
8: 2.9,
9: 3.0,
10: 3.0,
11: 3.1,
12: 3.2,
13: 3.2,
14: 3.3,
15: 3.3,
16: 3.4,
17: 3.5,
18: 3.6,
19: 3.6,
20: 3.7,
21: 3.8,
22: 3.9,
23: 3.9,
24: 4.0,
25: 4.1,
26: 4.2,
27: 4.2,
28: 4.3,
29: 4.4,
30: 4.5,
31: 4.5,
32: 4.6,
33: 4.7,
34: 4.8,
35: 4.9,
36: 5.0,
37: 5.1,
38: 5.1,
39: 5.2,
40: 5.3,
41: 5.4,
42: 5.5,
43: 5.6,
44: 5.7,
45: 5.7,
46: 5.8,
47: 5.9,
48: 6.0,
49: 6.1,
50: 6.1,
51: 6.2,
52: 6.3,
53: 6.4,
54: 6.5,
55: 6.5,
56: 6.6,
57: 6.7,
58: 6.8,
59: 6.9,
60: 7.0,
61: 7.0,
62: 7.1,
63: 7.2,
64: 7.3,
65: 7.4,
66: 7.5,
67: 7.5,
68: 7.6,
69: 7.7,
70: 7.8,
71: 10.8,
72: 10.8,
73: 10.8,
74: 10.8,
75: 10.9,
76: 10.9,
77: 10.9,
78: 10.9,
79: 10.9,
80: 10.9}}
df = pd.DataFrame(dataDict)
#Plotting
fig,ax = plt.subplots(figsize=(12,8))
sns.lineplot(x='Year',y='billion',data=df,ax=ax,color='b')
ax.set_ylim([2,11])
ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.margins(x=0, tight=True) # zero padding for the x-axis```
plt.fill_between(df['Year'], df['billion'], color='darkgreen', where=df['Year'] <= 2019)
plt.fill_between(df['Year'], df['billion'], color='lightgreen', where=df['Year'] >= 2019)
marked_years = [1950, 1987, 2019, 2050]
ax.scatter(marked_years, np.interp(marked_years, df['Year'], df['billion']), marker='o', color='black', s=50)
plt.text(1950,2.7,'2.5 Billion\nin 1950',horizontalalignment='right')
plt.text(1987,5.3,'5.1 Billion\nin 1987',horizontalalignment='right')
plt.text(2019,8,'7.7 Billion\nin 2019',horizontalalignment='right')
plt.text(2050,9.3,'9.7 Billion\nin 2050',horizontalalignment='right',color='r')
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)#hiding y spine
plt.gca().axes.get_yaxis().set_visible(False) #hiding y axis
ax.spines['right'].set_visible(False)
plt.savefig('worldPopulationGrowth_by2050.png',dpi=300,bbox_inches='tight')
plt.show()
plt.close()
将numpy导入为np
作为pd进口熊猫
导入seaborn作为sns
导入matplotlib
将matplotlib.pyplot作为plt导入
将matplotlib.ticker导入为ticker
从赛璐珞进口照相机
dataDict={'Year':{0:1950,
1: 1951,
2: 1952,
3: 1953,
4: 1954,
5: 1955,
6: 1956,
7: 1957,
8: 1958,
9: 1959,
10: 1960,
11: 1961,
12: 1962,
13: 1963,
14: 1964,
15: 1965,
16: 1966,
17: 1967,
18: 1968,
19: 1969,
20: 1970,
21: 1971,
22: 1972,
23: 1973,
24: 1974,
25: 1975,
26: 1976,
27: 1977,
28: 1978,
29: 1979,
30: 1980,
31: 1981,
32: 1982,
33: 1983,
34: 1984,
35: 1985,
36: 1986,
37: 1987,
38: 1988,
39: 1989,
40: 1990,
41: 1991,
42: 1992,
43: 1993,
44: 1994,
45: 1995,
46: 1996,
47: 1997,
48: 1998,
49: 1999,
50: 2000,
51: 2001,
52: 2002,
53: 2003,
54: 2004,
55: 2005,
56: 2006,
57: 2007,
58: 2008,
59: 2009,
60: 2010,
61: 2011,
62: 2012,
63: 2013,
64: 2014,
65: 2015,
66: 2016,
67: 2017,
68: 2018,
69: 2019,
70: 2020,
71: 2091,
72: 2092,
73: 2093,
74: 2094,
75: 2095,
76: 2096,
77: 2097,
78: 2098,
79: 2099,
80: 2100},
‘十亿’:{0:2.5,
1: 2.6,
2: 2.6,
3: 2.7,
4: 2.7,
5: 2.8,
6: 2.8,
7: 2.9,
8: 2.9,
9: 3.0,
10: 3.0,
11: 3.1,
12: 3.2,
13: 3.2,
14: 3.3,
15: 3.3,
16: 3.4,
17: 3.5,
18: 3.6,
19: 3.6,
20: 3.7,
21: 3.8,
22: 3.9,
23: 3.9,
24: 4.0,
25: 4.1,
26: 4.2,
27: 4.2,
28: 4.3,
29: 4.4,
30: 4.5,
31: 4.5,
32: 4.6,
33: 4.7,
34: 4.8,
35: 4.9,
36: 5.0,
37: 5.1,
38: 5.1,
39: 5.2,
40: 5.3,
41: 5.4,
42: 5.5,
43: 5.6,
44: 5.7,
45: 5.7,
46: 5.8,
47: 5.9,
48: 6.0,
49: 6.1,
50: 6.1,
51: 6.2,
52: 6.3,
53: 6.4,
54: 6.5,
55: 6.5,
56: 6.6,
57: 6.7,
58: 6.8,
59: 6.9,
60: 7.0,
61: 7.0,
62: 7.1,
63: 7.2,
64: 7.3,
65: 7.4,
66: 7.5,
67: 7.5,
68: 7.6,
69: 7.7,
70: 7.8,
71: 10.8,
72: 10.8,
73: 10.8,
74: 10.8,
75: 10.9,
76: 10.9,
77: 10.9,
78: 10.9,
79: 10.9,
80: 10.9}}
df=pd.DataFrame(dataDict)
#策划
图,ax=plt.子批次(图尺寸=(12,8))
sns.lineplot(x='Year',y='十亿',data=df,ax=ax,color='b')
ax.set_ylim([2,11])
ax.xaxis.set\u major\u定位器(股票代码多路传输器(10))
最大边距(x=0,紧密=真)#x轴的零填充```
在(df['Year',df['billion',color='darkgreen',其中=df['Year']=2019)之间填充
标记年份=[1950、1987、2019、2050]
最大散射(标记年,np.interp(标记年,df['Year',df['billion',]),标记为o',颜色为黑色,s=50)
plt.text(1950,2.7,'1950年为25亿,'horizontalalignment='right')
plt.text(1987年,5.3,'1987年,51亿,'horizontalalignment='right')
plt.text(2019,8,'7.7亿\n 2019',horizontalalignment='right')
plt.text(2050,9.3,'9.7十亿\n在2050年',水平对齐='right',颜色='r')
ax.spines['top'].set_可见(假)
ax.spines['left'].设置_可见(假)#隐藏y spine
plt.gca().axes.get_yaxis().set_visible(False)#隐藏y轴
ax.spines['右'].set_可见(假)
plt.savefig('worldPopulationGrowth\u By 2050.png',dpi=300,bbox\u inches='tight')
plt.show()
plt.close()
我尝试使用matplotlib文档()中的示例。
我可以只设置线条图的动画,但不能使用以下代码在颜色之间填充。\u:
#Tentative of animation
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x = df['Year']
y = df['billion']
fig, ax = plt.subplots()
line, = ax.plot(x, y, color='b')
#plt.fill_between(df['Year'], df['billion'], color='darkgreen', where=df['Year'] <= 2019)
#plt.fill_between(df['Year'], df['billion'], color='lightgreen', where=df['Year'] >= 2019)
def update(num, x, y, line):
line.set_data(x[:num], y[:num])
return line,
ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line],
interval=15, blit=True)
ani.save('populationAnimated.gif')
plt.show()
#动画的尝试
将numpy作为np导入
将matplotlib.pyplot作为plt导入
将matplotlib.animation导入为动画
x=df[‘年’]
y=df['十亿']
图,ax=plt.子批次()
直线,=ax.plot(x,y,color='b')
#在(df['Year',df['billion',color='darkgreen',其中=df['Year']=2019)之间填充
def更新(数字、x、y、行):
行。设置_数据(x[:num],y[:num])
回程线,
ani=animation.FuncAnimation(图,更新,len(x),fargs=[x,y,line],
间隔=15,blit=真)
ani.save('populationAnimated.gif')
plt.show()
因此,我正在寻求您的帮助,以动画与绿色之间的填充颜色的初始情节。提前感谢您抽出时间和分享您的知识。一系列修补程序存储在
集合中。每次调用更新时,都会添加一个新补丁。因此,您需要清除在此实现的以前的修补程序。此外,您希望使用blit=False
,因为正在修改轴
df=pd.DataFrame(dataDict)
x=df[‘年’]
y=df['十亿']
图,ax=plt.子批次()
直线,=ax.plot(x,y,color='b')
def更新(数量、x、y、行、ax):
ax.collections.clear()
行。设置_数据(x[:num],y[:num])
(df['Year'][:num],df['billion'][:num],color='darkgreen',其中=df['Year'][:num]2019)之间的最大填充量
返回线
ani=animation.FuncAnimation(图,更新,len(x),fargs=[x,y,line,ax],
间隔=15,blit=False)
ani.save('populationAnimated.gif')
plt.show()
df = pd.DataFrame(dataDict)
x = df['Year']
y = df['billion']
fig, ax = plt.subplots()
line, = ax.plot(x, y, color='b')
def update(num, x, y, line,ax):
ax.collections.clear()
line.set_data(x[:num], y[:num])
ax.fill_between(df['Year'][:num], df['billion'][:num], color='darkgreen', where=df['Year'][:num] <= 2019)
ax.fill_between(df['Year'][:num], df['billion'][:num], color='lightgreen', where=df['Year'][:num] > 2019)
return line, ax
ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line,ax],
interval=15, blit=False)
ani.save('populationAnimated.gif')
plt.show()