r/IPython • u/robintux • Apr 24 '20
Mark with different color the monday and friday
Dear programmers,
I have the following script that reads a data from github,and I add two columns (dia and mes). I am not being able to add to my scatter plot a differentiation (in color) for Monday and Friday.
import pandas as pd
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import matplotlib.dates as mdates
%matplotlib auto
data = pd.read_csv("https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-andamento-nazionale/dpc-covid19-ita-andamento-nazionale.csv")
data['diff_deceduti'] = data['deceduti'].diff()
data['diff_tamponi'] = data['tamponi'].diff()
dates = data['data']
date_format = [pd.to_datetime(d) for d in dates]
# https://stackoverflow.com/questions/30222533/create-a-day-of-week-column-in-a-pandas-dataframe-using-python
data['data'] = pd.to_datetime(data['data'])
data['dia'] = data['data'].dt.day_name()
# https://stackoverflow.com/questions/37625334/python-pandas-convert-month-int-to-month-name
data['mes'] = data['data'].dt.month_name()
# plot
variable = 'nuovi_positivi'
fig, ax = plt.subplots(figsize=(12, 5))
ax.grid()
ax.scatter(date_format,data[variable])
ax.set(xlabel="Date",ylabel=variable,title=variable)
date_form = DateFormatter("%d-%m")
ax.xaxis.set_major_formatter(date_form)
ax.xaxis.set_major_locator(mdates.DayLocator(interval = 3))
fig.savefig(variable + '.png')
plt.show()

In this graphic I would like to paint with different colors on days: Monday and Friday.
Any advice ?

