Created
May 2, 2018 09:21
-
-
Save mgbckr/0949cbc647aad7e80105a7da6b44983d to your computer and use it in GitHub Desktop.
A helper function to plot data with multiple axes using Python's matplotlib. Adjust to your needs. Obviously needs some clean up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plot_multiple_axes(data, columns, x= "timestamp", colors=["black", "r-", "g-", "c-", "m-", "y-"], window=60): | |
""" | |
TODO: Needs some clean up | |
:type data: pandas.DataFrame | |
:type columns: list of str | |
""" | |
fig, host = plt.subplots(figsize=(20, 4)) | |
axes = [host] + [host.twinx() for c in range(1, len(columns))] | |
# spines | |
for i in range(1,len(columns)): | |
axes[i].spines["right"].set_position(("axes", 1.0 + (i-1) * 0.07)) | |
# axis labels | |
for i in range(0,len(columns)): | |
axes[i].set_ylabel(columns[i]) | |
# plots | |
def draw(i): | |
if i is 0: | |
linewidth=2 | |
else: | |
linewidth=1 | |
p, = axes[i].plot(data[x], data[columns[i]].rolling(window=window,center=False).mean(), colors[i], label=columns[i], linewidth=linewidth) | |
axes[i].yaxis.label.set_color(p.get_color()) | |
return p | |
plots = [draw(i) for i in range(0,len(columns))] | |
host.legend(plots, [p.get_label() for p in plots]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment