就按照網上的教程,復制粘貼運行,根本不行。
自己根據說明修改了版本,已經是無錯版了,但是animation那一步始終做不了,為啥?
案例來自網路上的經典版本,是不是為了謝絕粘貼,所以在里面加了些錯誤。
之前有人叫把warning直接Ingnore,額,你牛
源代碼
import matplotlib.animation as ani
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
url = 'C:/Users/user1/Desktop/20201111/df.csv'
df = pd.read_csv(url, delimiter=',', header='infer')
df_interest = df.loc[df['Country/Region'].isin(['United Kingdom', 'US', 'Italy', 'Germany'])& df['Province/State'].isnull()].copy()
df_interest.rename(index=lambda x:df_interest.at[x, 'Country/Region'], inplace=True)
df1 = df_interest.transpose()
df1 = df1.drop(['Province/State', 'Country/Region', 'Lat', 'Long'])
df1 = df1.loc[(df1 != 0).any(1)]
df1.index = pd.to_datetime(df1.index)
color = ['red', 'green', 'blue', 'orange']
fig = plt.figure()
plt.xticks(rotation=45, ha="right", rotation_mode="anchor") #rotate the x-axis values
plt.subplots_adjust(bottom = 0.2, top = 0.9) #ensuring the dates (on the x-axis) fit in the screen
plt.ylabel('No of Deaths')
plt.xlabel('Datas')
def buildmebarchart(i = int):
plt.legend(df1.columns)
p = plt.plot(df1[:i].index, df1[:i].values) #note it only returns the dataset, up to the point i
for i in range(0,4):
p[i].set_color(color[i]) #set the color of each curve
plt.rcParams['animation.ffmpeg_path'] = 'C:/ffmpeg/bin/ffmpeg.exe'
animator = ani.FuncAnimation(fig, buildmebarchart, interval = 100)
plt.show()
animator.save('C:/temp/myfirstAnimation.gif',writer='pillow')
無法保存動圖,然后Plt.show()也是啥都沒顯示出來。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/225127.html
