Created
November 23, 2018 10:04
-
-
Save korkridake/f0c6b6f86350b23db1148f6e5232fad8 to your computer and use it in GitHub Desktop.
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
# ------------------------------------------------------------------------- | |
# ------------------------------------------------------------------------- | |
# Matplotlib Twin Axis Plots | |
# Description: plot histogram and line chart together in a dual-axis approach | |
# ------------------------------------------------------------------------- | |
# ------------------------------------------------------------------------- | |
import matplotlib.pyplot as plt | |
import seaborn as sns; | |
sns.set(style="whitegrid", color_codes=True) | |
%matplotlib inline | |
bins = range(0, [INSERT SOME VALUES]) # set for bar chart | |
fig, ax1 = plt.subplots(figsize=(10,8)) | |
# ------------------------------------------------------------------------- | |
# For the First Plot (Histogram) | |
# ------------------------------------------------------------------------- | |
ax1.hist(df['INSERT THE CATEGORICAL VARIABLE'].values, bins=bins, edgecolor='k', align='left'); | |
ax1.set_ylabel('[INSERT YOUR Y_LABEL Of THE FIRST PLOT]') | |
# ------------------------------------------------------------------------- | |
# For the Second Plot (Line Chart) | |
# ------------------------------------------------------------------------- | |
ax2 = ax1.twinx() | |
ax2.plot(df['INSERT THE CATEGORICAL VARIABLE (SAME AS ABOVE)'].values, df['INSERT THE NUMERICAL VARIABLE'].values, marker='x', color='r') | |
ax2.set_ylabel('[INSERT YOUR Y_LABEL Of THE SECOND PLOT]', color='r') | |
ax2.tick_params('y', colors='r') | |
plt.title('[INSERT YOUR TITLE]') | |
plt.tight_layout() | |
plt.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment