import matplotlib.pyplot as plt
plt.style.use('classic')
import numpy as np
%matplotlib inline
x = np.random.randn(1000)
plt.hist(x);
# use a gray background
ax = plt.axes(axisbg='#E6E6E6')
ax.set_axisbelow(True)
# draw solid white grid lines
plt.grid(color='w', linestyle='solid')
# hide axis spines
for spine in ax.spines.values():
spine.set_visible(False)
# hide top and right ticks
ax.xaxis.tick_bottom()
ax.yaxis.tick_left()
# lighten ticks and labels
ax.tick_params(colors='gray', direction='out')
for tick in ax.get_xticklabels():
tick.set_color('gray')
for tick in ax.get_yticklabels():
tick.set_color('gray')
# control face and edge color of histogram
ax.hist(x, edgecolor='#E6E6E6', color='#EE6666');
rcParams
plt.rc
convenience routine.
Let's see what it looks like to modify the rc parameters so that our default plot will look similar to what we did before.rcParams
dictionary, so we can easily reset these changes in the current session:IPython_default = plt.rcParams.copy()
plt.rc
function to change some of these settings:from matplotlib import cycler
colors = cycler('color',
['#EE6666', '#3388BB', '#9988DD',
'#EECC55', '#88BB44', '#FFBBBB'])
plt.rc('axes', facecolor='#E6E6E6', edgecolor='none',
axisbelow=True, grid=True, prop_cycle=colors)
plt.rc('grid', color='w', linestyle='solid')
plt.rc('xtick', direction='out', color='gray')
plt.rc('ytick', direction='out', color='gray')
plt.rc('patch', edgecolor='#E6E6E6')
plt.rc('lines', linewidth=2)
plt.hist(x);
for i in range(4):
plt.plot(np.random.rand(10))
style
module, which includes a number of new default stylesheets, as well as the ability to create and package your own styles. These stylesheets are formatted similarly to the .matplotlibrc files mentioned earlier, but must be named with a .mplstyle extension.plt.style.available
—here I'll list only the first five for brevity:plt.style.available[:5]
plt.style.use('stylename')
with plt.style.context('stylename'):
make_a_plot()
def hist_and_lines():
np.random.seed(0)
fig, ax = plt.subplots(1, 2, figsize=(11, 4))
ax[0].hist(np.random.randn(1000))
for i in range(3):
ax[1].plot(np.random.rand(10))
ax[1].legend(['a', 'b', 'c'], loc='lower left')
# reset rcParams
plt.rcParams.update(IPython_default);
hist_and_lines()
fivethirtyeight
style mimics the graphics found on the popular FiveThirtyEight website.
As you can see here, it is typified by bold colors, thick lines, and transparent axes:with plt.style.context('fivethirtyeight'):
hist_and_lines()
ggplot
package in the R language is a very popular visualization tool.
Matplotlib's ggplot
style mimics the default styles from that package:with plt.style.context('ggplot'):
hist_and_lines()
bmh
stylesheet:with plt.style.context('bmh'):
hist_and_lines()
dark_background
style provides this:with plt.style.context('dark_background'):
hist_and_lines()
grayscale
style, shown here, can be very useful:with plt.style.context('grayscale'):
hist_and_lines()
import seaborn
hist_and_lines()