Python 如何在Matplotlib上一个接一个地绘制点?
我想计算几年内爱尔兰的gdp与预期寿命之比。我想在散点图上绘制第一个点,然后等待几秒钟,绘制下一个点Python 如何在Matplotlib上一个接一个地绘制点?,python,matplotlib,scatter-plot,Python,Matplotlib,Scatter Plot,我想计算几年内爱尔兰的gdp与预期寿命之比。我想在散点图上绘制第一个点,然后等待几秒钟,绘制下一个点 ax = plt.figure() for i in year: plt.scatter(ie_gdp[i], ie_life[i]) plt.draw() plt.show() plt.pause(1) 到目前为止,我只能想到这些。但是,使用JupterLab可以为每个点绘制单独的绘图。我试过在线观看动画,但它们都使用实时数据。我已经在ie_gdp和ie_
ax = plt.figure()
for i in year:
plt.scatter(ie_gdp[i], ie_life[i])
plt.draw()
plt.show()
plt.pause(1)
到目前为止,我只能想到这些。但是,使用JupterLab可以为每个点绘制单独的绘图。我试过在线观看动画,但它们都使用实时数据。我已经在ie_gdp和ie_生活中清理了数据集
%matplotlib inline
fig = plt.figure(figsize = (15,15))
ax = fig.add_subplot(1,1,1)
def animate(i):
xs = []
ys = []
for y in year:
xs.append(ie_gdp[y])
ys.append(ie_life[y])
ax.cla()
ax.scatter(xs,ys)
ani = animation.FuncAnimation(fig, animate, interval = 10000)
plt.show()
以上是我尝试使用动画,但它也不起作用。我收到以下错误:AttributeError:“list”对象没有属性“shape”
任何帮助都将不胜感激。我不确定我是否理解您想要的动画,但我将x轴设置为年,y轴设置为平均年龄,散点图的大小设置为GDP值。样本数据来自Plotly提供的数据,因此请用您自己的数据替换
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
#from IPython.display import HTML
#from matplotlib.animation import PillowWriter
# for sample data
import plotly.express as px
df = px.data.gapminder()
ie_df = df[df['country'] == 'Ireland']
ie_df
country continent year lifeExp pop gdpPercap iso_alpha iso_num
744 Ireland Europe 1952 66.910 2952156 5210.280328 IRL 372
745 Ireland Europe 1957 68.900 2878220 5599.077872 IRL 372
746 Ireland Europe 1962 70.290 2830000 6631.597314 IRL 372
747 Ireland Europe 1967 71.080 2900100 7655.568963 IRL 372
748 Ireland Europe 1972 71.280 3024400 9530.772896 IRL 372
749 Ireland Europe 1977 72.030 3271900 11150.981130 IRL 372
750 Ireland Europe 1982 73.100 3480000 12618.321410 IRL 372
751 Ireland Europe 1987 74.360 3539900 13872.866520 IRL 372
752 Ireland Europe 1992 75.467 3557761 17558.815550 IRL 372
753 Ireland Europe 1997 76.122 3667233 24521.947130 IRL 372
754 Ireland Europe 2002 77.783 3879155 34077.049390 IRL 372
755 Ireland Europe 2007 78.885 4109086 40675.996350 IRL 372
fig = plt.figure(figsize=(10,10))
ax = plt.axes(xlim=(1952,2012), ylim=(0, 45000))
scat = ax.scatter([], [], [], cmap='jet')
def animate(i):
tmp = ie_df.iloc[:i,:]
ax.clear()
scat = ax.scatter(tmp['year'], tmp['lifeExp'], s=tmp['gdpPercap'], c=tmp['gdpPercap'], ec='k')
ax.set_xlabel('Year', fontsize=15)
ax.set_ylabel('lifeExp', fontsize=15)
ax.set_title('Ireland(1952-2007)')
return scat,
anim = FuncAnimation(fig, animate, frames=12, interval=1000, repeat=False)
#anim.save('gdp_life.gif', writer='Pillow')
plt.show()
您的具体问题是什么?交互式模式对于您的任务来说已经足够了。使用ion示例,我得到以下错误:UserWarning:Matplotlib当前正在使用module://ipykernel.pylab.backend_inline,它是非GUI后端,因此无法显示该图。策略一:。策略二:使用动画。