I've recently started using the dark chesterish
theme from dunovank, and I
love how good a simple pandas.DataFrame.plot() looks like out of the box:
# Theme from dunovank, exclude if not installed:
from jupyterthemes import jtplot
jtplot.style()
# snippet from pandas docs:
ts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2000', periods=1000)).cumsum()
ax = ts.plot()
Output 1:
But I'd like to add an alternating background color (seems to be all the rage with big news agencies). The post How can I set the background color on specific areas of a pyplot figure? gives a good description of how you can do it. And it's really easy for numeric x-values:
Snippet 2:
# imports
import pandas as pd
import numpy as np
from jupyterthemes import jtplot
# Sample data
np.random.seed(123)
rows = 50
dfx = pd.DataFrame(np.random.randint(90,110,size=(rows, 1)), columns=['Variable Y'])
dfy = pd.DataFrame(np.random.randint(25,68,size=(rows, 1)), columns=['Variable X'])
df = pd.concat([dfx,dfy], axis = 1)
jtplot.style()
ax = df.plot()
for i in range(0, 60, 20):
ax.axvspan(i, i+10, facecolor='lightgrey', alpha=0.025)
Output 2:
But it gets a lot messier (for me at least) when the x-axis is of a time or date format. And that's because the axis in my two examples goes from this
# in:
ax.lines[0].get_data()
# out:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
dtype=int64)
To this (abbreviated):
# in:
ts.plot().lines[0].get_data()
# out:
.
.
Period('2002-09-15', 'D'), Period('2002-09-16', 'D'),
Period('2002-09-17', 'D'), Period('2002-09-18', 'D'),
Period('2002-09-19', 'D'), Period('2002-09-20', 'D'),
Period('2002-09-21', 'D'), Period('2002-09-22', 'D'),
Period('2002-09-23', 'D'), Period('2002-09-24', 'D'),
Period('2002-09-25', 'D'), Period('2002-09-26', 'D')], dtype=object)
ts.plot().lines[0].get_data()
returns the data on the x-axis. But is there a way to find out where matplotlib renders the vertical lines for each 'Jan' observation, so I can more easily find decent intervals for the alternating black and grey background color?
Thank you for any suggestions!
Edit - Or is there a theme?
Or does anyone know if there exists a theme somewhere that is free to use?
I've checked all matplotlib themes import matplotlib.pyplot as plt; print(plt.style.available)
and Seaborn, but with no success.
Edit 2 - Suggested solution from ImportanceOfBeingErnest with the chesterish theme activated:
In my humble opinion, this is a perfect setup for a time series chart (could maybe drop the splines though)