Style Sheets

Style Sheets#

The prospect of having to make choices about font, style, color, and formatting for every component of your plot can be daunting. matplotlib includes a wide range of predefined styles. A few examples:

Similar to how CSS (cascading style sheets) interact with HTML (hyper-text markup language), these style sheets cover style and formatting elements like background colors, gridlines, line widths, fonts, font sizes, and more. To use one of these styles, we can add a single line of code before starting to generate the plot.

%matplotlib inline
import matplotlib.pyplot as plt # import statement

plt.style.use('ggplot') # set style sheet

squares = [1,4,9,16,25] # dataset for y axis
inputs = [1,2,3,4,5] # dataset for x axis

fig, ax = plt.subplots() # figure for new plot
ax.plot(inputs, squares) # generate plot

ax.set_title("Square Numbers") # set plot title
ax.set_xlabel("Value") # set x axis label
ax.set_ylabel("Square of Value") # set y axis label

plt.show() # show output

Additional Resources#

Consult the matplotlib “Style sheets reference” page to learn more.

For those interested in data journalism, most large publications have an internal style guide. And since 2017, the AP Stylebook has included a chapter on data journalism.