简体   繁体   English

如何从 Matplotlib 和 plot 上的时间序列图中显示日期

[英]How to show date from a time-series graph on Matplotlib a plot

I have this code-base to plotting some time-series (you can see it at the url ).我有这个代码库来绘制一些时间序列(你可以在url看到它)。

I've been trying to show the raw dates, but I can only get the days numbered (from 1 to the last), even if the time-series show dates by itself.我一直在尝试显示原始日期,但我只能获得编号的日期(从 1 到最后一个),即使时间序列本身显示日期。 By the time I'm writing this, I've tried different solutions to change these numbered days to actual dates without any luck.在我写这篇文章的时候,我已经尝试了不同的解决方案来将这些数字日期更改为实际日期,但没有任何运气。 So I'm coming to guys for help.所以我来找人帮忙。 How can I solve this?我该如何解决这个问题? Even the recommended theory will help in order to improve my understanding.即使是推荐的理论也将有助于提高我的理解。

This is the current code:这是当前代码:

import pandas as pd
from datetime import datetime, timedelta
import matplotlib.dates as mpl_dates
import geopandas as gpd
import matplotlib.pyplot as plt 
plt.style.use('seaborn')
%matplotlib qt

url = 'https://raw.githubusercontent.com/mariorz/covid19-mx-time-series/master/data/covid19_confirmed_mx.csv'
df = pd.read_csv(url, index_col=0)
df = df.loc['Colima','18-03-2020':'26-06-2020']
df.index = pd.to_datetime(df.index, format='%d-%m-%Y')

df.reset_index(inplace=True, drop=True)
fig, ax = plt.subplots()
fig.text(0.90, 0.17, 'datacol.com.mx',
          fontsize=8, color='gray',
          ha='right', va='bottom', alpha=0.3)
plt.xlabel('Días desde el primer caso positivo en Colima (18 de marzo, 2020)', fontsize=10)
plt.ylabel('Casos positivos', fontsize=10)
plt.title('Casos positivos acumulados de COVID-19 en Colima (26 de junio, 2020)', fontsize=10)
plt.tight_layout()
df.plot()
plt.savefig('viz/casos_acumulados.png', dpi=400)

I thank you guys in advance.我提前感谢你们。

Since df is a series, it is converted to data frames.由于 df 是一个系列,它被转换为数据帧。 Also, the x-axis is a time series, so we have not reset the index.另外,x 轴是时间序列,所以我们没有重置索引。 I'm only posting the difference between your code and the difference.我只是发布您的代码和差异之间的差异。

df = pd.DataFrame(df)
df.index = pd.to_datetime(df.index, format='%d-%m-%Y')

# df.reset_index(inplace=True, drop=True)

full code:完整代码:

import pandas as pd
from datetime import datetime, timedelta
import matplotlib.dates as mpl_dates
import geopandas as gpd
import matplotlib.pyplot as plt 
plt.style.use('seaborn')
# %matplotlib qt

url = 'https://raw.githubusercontent.com/mariorz/covid19-mx-time-series/master/data/covid19_confirmed_mx.csv'
df = pd.read_csv(url, index_col=0)
df = df.loc['Colima','18-03-2020':'26-06-2020']
df = pd.DataFrame(df)
df.index = pd.to_datetime(df.index, format='%d-%m-%Y')

# df.reset_index(inplace=True, drop=True)
fig, ax = plt.subplots()
ax.plot(df.Colima)
ax.text(max(df.index), 0.17, 'datacol.com.mx', fontsize=8, color='gray', ha='right', va='bottom', alpha=0.3)
ax.set_xlabel('Días desde el primer caso positivo en Colima (18 de marzo, 2020)', fontsize=10)
ax.set_ylabel('Casos positivos', fontsize=10)
ax.set_title('Casos positivos acumulados de COVID-19 en Colima (26 de junio, 2020)', fontsize=10)

plt.tight_layout()
# ax = df.plot()
plt.savefig('casos_acumulados.png', dpi=400)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM