The question:
I'm trying to grasp the concept of monkey patching and at the same time make a function to produce the perfect time-series plot. How can I include the following matplotlib functionality in pandas pandas.DataFrame.plot()?
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
complete code at the end of the question
The details:
I think the default settings in df.plot() is pretty neat, especially if you're running a Jupyter Notebook with a dark theme like chesterish from dunovank:
And I'd like to use it for as much of my data analysis workflow as possible, but I'd really like to remove the frame (or what's called spines) like this:
Arguably, this is a perfect time-series plot. But df.plot() doesn't have a built-in argument for this. The closest thing seems to be grid = False, but that takes away the whole grid in the same run:
What I've tried
I know I can wrap the spine snippet in a function along with df.plot() so I end up with this:
Snippet 1:
def plotPerfect(df, spline):
ax = df.plot()
if not spline:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
return(ax)
plotPerfect(df = df, spline = False)
Output 1:
But is that the "best" way to do it with regards to flexibilty and readability for future amendments? Or even the fastest with regards to execution time if we're talking hundreds of plots?
I know how I can get the df.plot() source, but everything from there leaves me baffled. So how do I include those settings in df.plot? And perhaps the wrapped function approach is just as good as monkey patching?
Snippet with full code and sample data:
To reproduce the example 100%, paste this into a Jupyter Notebook cell with the chesterish theme activated:
# 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()
# Plot with default settings
df.plot()
# Wrap df.plot() and matplotlib spine in a function
def plotPerfect(df, spline):
ax = df.plot()
if not spline:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
return(ax)
# Plot the perfect time-series plot
plotPerfect(df = df, spline = False)





