Skip to content

Instantly share code, notes, and snippets.

@dyerrington
Created March 29, 2017 21:33
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save dyerrington/dac39db54161dafc9359995924413a12 to your computer and use it in GitHub Desktop.
Save dyerrington/dac39db54161dafc9359995924413a12 to your computer and use it in GitHub Desktop.
Plotting multiple figures with seaborn and matplotlib using subplots.
##
# Create a figure space matrix consisting of 3 columns and 2 rows
#
# Here is a useful template to use for working with subplots.
#
##################################################################
fig, ax = plt.subplots(figsize=(10,5), ncols=3, nrows=2)
left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = .5 # the amount of width reserved for blank space between subplots
hspace = 1.1 # the amount of height reserved for white space between subplots
# This function actually adjusts the sub plots using the above paramters
plt.subplots_adjust(
left = left,
bottom = bottom,
right = right,
top = top,
wspace = wspace,
hspace = hspace
)
# The amount of space above titles
y_title_margin = 1.2
plt.suptitle("Original vs Normalized vs Standardized", y = 1.09, fontsize=20)
### Bathrooms
ax[0][0].set_title("Original", y = y_title_margin)
ax[0][1].set_title("Normalized (MinMax)", y = y_title_margin)
ax[0][2].set_title("Starndardized (StardardScaler)", y = y_title_margin)
sns.distplot(df['bathrooms'], kde = False, ax=ax[0][0])
sns.distplot(df['norm_bathrooms'], kde = False, ax=ax[0][1])
sns.distplot(df['stand_bathrooms'], kde = False, ax=ax[0][2])
# Set all labels on the row axis of subplots for bathroom data to "bathrooms"
[ax[0][i].set_xlabel("bathrooms") for i in range(0, 3)]
### Square feet
ax[1][0].set_title("Original", y = y_title_margin)
ax[1][1].set_title("Normalized (MinMax)", y = y_title_margin)
ax[1][2].set_title("Standardized (StandardScaler)", y=y_title_margin)
sns.distplot(df['square_feet'], kde = False, ax=ax[1][0])
sns.distplot(df['norm_square_feet'], kde = False, ax=ax[1][1])
sns.distplot(df['stand_square_feet'],kde = False, ax=ax[1][2])
# Set all labels on the row axis of subplots for square_feet data to "square_feet"
[ax[1][i].set_xlabel("square_feet") for i in range(0, 3)]
@goethan
Copy link

goethan commented Apr 9, 2018

Hello, thank you very much for sharing the code.

When I tried to reproduce it, an error occurs:

C:\Users\Anaconda2\lib\site-packages\matplotlib\axes_axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
5981 color = mcolors.colorConverter.to_rgba_array(color)
5982 if len(color) != nx:
-> 5983 raise ValueError("color kwarg must have one color per dataset")
5984
5985 # Save the datalimits for the same reason:

ValueError: color kwarg must have one color per dataset

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment