I have a database from COVID that I'm playing with. I have info in the format YYYY-MM-DD and I'm plotting days vs. number of cases in a bar plot. In my graph, the days appear as months, but I want an axis with every 7 days. Should I convert my datetime64 format to MM-DD and plot my graph?
This is the code I'm using to plot:
fig, ax = plt.subplots(figsize=(15,7))
ax.bar(x0, y0, color='green')
ax.set(xlabel = 'Data da notificação',
ylabel = 'Casos novos')
plt.setp(ax.get_xticklabels(), rotation = 45)
plt.show()
And this is the resulted graph:
This is the graph I want:
Sample data: https://docs.google.com/spreadsheets/d/1q4Njy0mGMtIeEABdaFrzNEMSavjpXtlUn8BM8z_QZBk/edit?usp=sharing
To format like MM-DD
, you can specify fmt
argument which accepts a strftime format string for formatting and is a required argument to the DateFormatter
.
You can also specify an interval
argument to the DateLocator
. e.g. interval=7
the locator places ticks at every 7th date.
import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
df = pd.read_csv("1.csv", delim_whitespace=True)
df['data'] = pd.to_datetime(df['data'], format='%m/%d/%Y')
fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(df['data'], df['casosNovos'])
ax.xaxis.set_major_locator(mdates.DayLocator(interval=7))
ax.xaxis.set_major_formatter(mdates.DateFormatter(fmt='%m%d'))
ax.set_xlim(df['data'].min(), datetime.date(2020, 8, 21))
plt.setp(ax.get_xticklabels(), rotation = 45)
plt.show()
1.csv
looks like
regiao data casosNovos
Brasil 2/25/2020 0
Brasil 2/26/2020 1
Brasil 2/27/2020 0
Brasil 2/28/2020 0
Brasil 2/29/2020 1
Brasil 3/1/2020 0
Brasil 3/2/2020 0
Brasil 3/3/2020 0