Almost every element in a Seaborn figure can be further customized via seaborn.set. Here is the list of parameters that are supported:
- context: One of the preset contexts--{paper, notebook, talk, poster}.
- style: One of the axes' styles--{darkgrid, whitegrid, dark, white, ticks}.
- palette: One of the color palettes as defined in https://seaborn.pydata.org/generated/seaborn.color_palette.html#seaborn.color_palette.
- font: A supported font or font family name, such as serif, sans-serif, cursive, fantasy, or monospace. For more information, visit https://matplotlib.org/api/font_manager_api.html.
- font_scale: An independent scaling factor of font elements.
- rc: A dictionary of extra rc parameters mappings. To obtain the full list of all rc parameters, we can run seaborn.axes_style().
Let's try to increase the font scale, increase the line width of the KDE plot, and change the color of several plot elements:
# Get a dictionary of all parameters that can be changed
sns.axes_style()
"""
Returns
{'axes.axisbelow': True,
'axes.edgecolor': '.8',
'axes.facecolor': 'white',
'axes.grid': True,
'axes.labelcolor': '.15',
'axes.linewidth': 1.0,
'figure.facecolor': 'white',
'font.family': [u'sans-serif'],
'font.sans-serif': [u'Arial',
u'DejaVu Sans',
u'Liberation Sans',
u'Bitstream Vera Sans',
u'sans-serif'],
'grid.color': '.8',
'grid.linestyle': u'-',
'image.cmap': u'rocket',
'legend.frameon': False,
'legend.numpoints': 1,
'legend.scatterpoints': 1,
'lines.solid_capstyle': u'round',
'text.color': '.15',
'xtick.color': '.15',
'xtick.direction': u'out',
'xtick.major.size': 0.0,
'xtick.minor.size': 0.0,
'ytick.color': '.15',
'ytick.direction': u'out',
'ytick.major.size': 0.0,
'ytick.minor.size': 0.0}
"""
# Increase the font scale to 2, change the grid color to light grey,
# and axes label color to dark blue
sns.set(context="notebook",
style="darkgrid",
font_scale=2,
rc={'grid.color': '0.6',
'axes.labelcolor':'darkblue',
"lines.linewidth": 2.5})
# Plot the histogram
ax = sns.distplot(current_bigmac.dollar_price)
plt.show()
The code generates the following histogram:
So far, only functions that control global aesthetics were introduced. What if we want to change the style of a specific plot only?
Luckily, most Seaborn plotting functions come with specific parameters for the customization of styles. This also means that there isn't a universal styling tutorial for all Seaborn plotting functions. However, we can take a closer look at this seaborn.distplot() code excerpt to get an idea:
# Note: Codes related to data preparation and imports are skipped for
# brevity
# Reset the style
sns.set(context="notebook", style="darkgrid")
# Plot the histogram with custom style
ax = sns.distplot(current_bigmac.dollar_price,
kde_kws={"color": "g",
"linewidth": 3,
"label": "KDE"},
hist_kws={"histtype": "step",
"alpha": 1,
"color": "k",
"label": "histogram"})
plt.show()
The expected result:
Some Seaborn functions support a more direct approach of customizing aesthetics. For example, seaborn.barplot can pass through keyword arguments such as facecolor, edgecolor, ecolor, and linewidth to the underlying matplotlib.pyplot.bar function:
# Note: Codes related to data preparation and imports are skipped
# for brevity
# Population Bar chart
sns.barplot(x="AgeGrp",y="Value", hue="Sex",
linewidth=2, edgecolor="w",
data = current_population)
# Use Matplotlib functions to label axes rotate tick labels
ax = plt.gca()
ax.set(xlabel="Age Group", ylabel="Population (thousands)")
ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=45)
plt.title("Population Barchart (USA)")
# Show the figure
plt.show()