Reputation: 303
I have this code-base to plotting some time-series (you can see it at the 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. 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.
Upvotes: 0
Views: 278
Reputation: 35275
Since df is a series, it is converted to data frames. Also, the x-axis is a time series, so we have not reset the index. 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)
Upvotes: 1