【发布时间】:2020-09-03 10:43:34
【问题描述】:
enter image description here 你好,
我正在尝试重新创建我们看到的一些 covid-19 图表。我正在使用来自约翰霍普金斯大学数据库的数据。
数据的排列方式是城市名称在行中,列是日期。附上 csv 文件的屏幕截图。我想在 seaborn 中绘制折线图,其中 x 轴为天,y 轴为城市确认病例。由于某种原因,我无法重现死亡率的指数曲线。
我的代码是:
'''loading the file'''
date_columns = list(range(12,123))
df_covid_us = pd.read_csv(covid_us_file, parse_dates=date_columns)
df_covid_us = pd.read_csv(covid_us_file)
'''slicing the columns needed. Province_State and the date columns'''
df = df_covid_us.iloc[:, np.r_[6, 12:123]]
df = df[df['Province_State']=='New York']
'''using df.melt'''
df2 =df.melt(id_vars='Province_State',var_name='Date',value_name='Deaths')
'''plotting using seaborn'''[enter image description here][2]
sns.lineplot(x='Date',y='Deaths',data=df2, ci=None)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=20))
plt.show()
【问题讨论】:
-
请始终在您的问题中将您的 DataFrame 示例作为文本发布,而不是作为图片发布。这有助于人们帮助你。
-
嗨:如何发布我的数据框样本?我正在使用pycharm。谢谢
-
你可以做
df.head(3)左右。无论如何,我发布了一个带有虚构数据的答案。