Skip to content

Add 3 new styles with color schemes from Tableau [backport to 1.4.x] #3700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
78 changes: 78 additions & 0 deletions examples/showcase/bachelors_degrees_by_gender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import matplotlib.pyplot as plt
from matplotlib.mlab import csv2rec
from matplotlib.cbook import get_sample_data

# Activate the Tableau 20 and clean styling.
# This call does most of the styling for you.
plt.style.use(["clean", "tableau_20colors"])

fname = get_sample_data("percent_bachelors_degrees_women_usa.csv")
gender_degree_data = csv2rec(fname)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can put the file in lib/matplotlib/mpl-data/sample_data, and then access it using matplotlib.cbook.get_sample_data(). See the example pylab_examples/loadrec.py. Then you can read it using matplotlib.mlab.csv2rec.

from matplotlib.mlab import csv2rec
from matplotlib.cbook import get_sample_data
fname = get_sample_data('percent_bachelors_degrees_women_usa.csv')
gender_degree_data = csv2rec(fname)

That leaves you with a recarray, which supports both dictionary and attribute styles of field access.

# You typically want your plot to be ~1.33x wider than tall. This plot
# is a rare exception because of the number of lines being plotted on it.
# Common sizes: (10, 7.5) and (12, 9)
plt.figure(figsize=(12, 14))

# Limit the range of the plot to only where the data is.
# Avoid unnecessary whitespace.
plt.xlim(1970, 2010.1)
plt.ylim(-0.25, 90)

# Make sure your axis ticks are large enough to be easily read.
# You don't want your viewers squinting to read your plot.
plt.xticks(range(1970, 2011, 10))
plt.yticks(range(0, 91, 10), [str(x) + "%" for x in range(0, 91, 10)])

# Now that the plot is prepared, it's time to actually plot the data!
# Note that I plotted the majors in order of the highest % in the final year.
majors = ["Health Professions", "Public Administration", "Education",
"Psychology", "Foreign Languages", "English",
"Communications\nand Journalism", "Art and Performance", "Biology",
"Agriculture", "Social Sciences and History", "Business",
"Math and Statistics", "Architecture", "Physical Sciences",
"Computer Science", "Engineering"]

y_offsets = {"Foreign Languages": 0.5, "English": -0.5,
"Communications\nand Journalism": 0.75,
"Art and Performance": -0.25, "Agriculture": 1.25,
"Social Sciences and History": 0.25, "Business": -0.75,
"Math and Statistics": 0.75, "Architecture": -0.75,
"Computer Science": 0.75, "Engineering": -0.25}

for rank, column in enumerate(majors):
# Plot each line separately with its own color, using the Tableau 20
# color set in order.
column_rec_name = column.replace("\n", "_").replace(" ", "_").lower()

line = plt.plot(gender_degree_data.year,
gender_degree_data[column_rec_name])

line_color = line[0].get_color()

# Add a text label to the right end of every line. Most of the code below
# is adding specific offsets y position because some labels overlapped.
y_pos = gender_degree_data[column_rec_name][-1] - 0.5

if column in y_offsets:
y_pos += y_offsets[column]

# Again, make sure that all labels are large enough to be easily read
# by the viewer.
plt.text(2010.5, y_pos, column, color=line_color)

# matplotlib's title() call centers the title on the plot, but not the graph,
# so I used the text() call to customize where the title goes.

# Make the title big enough so it spans the entire plot, but don"t make it
# so big that it requires two lines to show.

# Note that if the title is descriptive enough, it is unnecessary to include
# axis labels; they are self-evident, in this plot's case.
plt.text(1995, 92, "Percentage of Bachelor's degrees conferred to women in "
"the U.S.A. by major (1970-2010)", fontsize=17, ha="center")

# Finally, save the figure as a PNG.
# You can also save it as a PDF, JPEG, etc.
# Just change the file extension in this call.
plt.savefig("percent-bachelors-degrees-women-usa.png")
19 changes: 19 additions & 0 deletions examples/style_sheets/plot_clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
This shows an example of the "clean" styling.
"""


from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(0, 10)

with plt.style.context('clean'):
plt.plot(x, np.sin(x) + x + np.random.randn(50))
plt.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 3 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 4 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 5 * x + np.random.randn(50))

plt.show()
20 changes: 20 additions & 0 deletions examples/style_sheets/plot_colorblind_10colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
This shows an example of the "colorblind_10colors" styling,
which uses Tableau's "Color Blind 10" color scheme.
"""


from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(0, 10)

with plt.style.context('colorblind_10colors'):
plt.plot(x, np.sin(x) + x + np.random.randn(50))
plt.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 3 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 4 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 5 * x + np.random.randn(50))

plt.show()
20 changes: 20 additions & 0 deletions examples/style_sheets/plot_tableau_10colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
This shows an example of the "tableau_10colors" styling,
which uses Tableau's "Tableau 10" color scheme.
"""


from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(0, 10)

with plt.style.context('tableau_10colors'):
plt.plot(x, np.sin(x) + x + np.random.randn(50))
plt.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 3 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 4 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 5 * x + np.random.randn(50))

plt.show()
20 changes: 20 additions & 0 deletions examples/style_sheets/plot_tableau_20colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
This shows an example of the "tableau_20colors" styling,
which uses Tableau's "Tableau 20" color scheme.
"""


from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(0, 10)

with plt.style.context('tableau_20colors'):
plt.plot(x, np.sin(x) + x + np.random.randn(50))
plt.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 3 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 4 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 5 * x + np.random.randn(50))

plt.show()
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Year,Agriculture,Architecture,Art and Performance,Biology,Business,Communications and Journalism,Computer Science,Education,Engineering,English,Foreign Languages,Health Professions,Math and Statistics,Physical Sciences,Psychology,Public Administration,Social Sciences and History
1970,4.22979798,11.92100539,59.7,29.08836297,9.064438975,35.3,13.6,74.53532758,0.8,65.57092343,73.8,77.1,38,13.8,44.4,68.4,36.8
1971,5.452796685,12.00310559,59.9,29.39440285,9.503186594,35.5,13.6,74.14920369,1,64.55648516,73.9,75.5,39,14.9,46.2,65.5,36.2
1972,7.42071022,13.21459351,60.4,29.81022105,10.5589621,36.6,14.9,73.55451996,1.2,63.6642632,74.6,76.9,40.2,14.8,47.6,62.6,36.1
1973,9.653602412,14.7916134,60.2,31.14791477,12.80460152,38.4,16.4,73.50181443,1.6,62.94150212,74.9,77.4,40.9,16.5,50.4,64.3,36.4
1974,14.07462346,17.44468758,61.9,32.99618284,16.20485038,40.5,18.9,73.33681143,2.2,62.41341209,75.3,77.9,41.8,18.2,52.6,66.1,37.3
1975,18.33316153,19.13404767,60.9,34.44990213,19.68624931,41.5,19.8,72.80185448,3.2,61.64720641,75,78.9,40.7,19.1,54.5,63,37.7
1976,22.25276005,21.39449143,61.3,36.07287146,23.4300375,44.3,23.9,72.16652471,4.5,62.14819377,74.4,79.2,41.5,20,56.9,65.6,39.2
1977,24.6401766,23.74054054,62,38.33138629,27.16342715,46.9,25.7,72.45639481,6.8,62.72306675,74.3,80.5,41.1,21.3,59,69.3,40.5
1978,27.14619175,25.84923973,62.5,40.11249564,30.52751868,49.9,28.1,73.19282134,8.4,63.61912216,74.3,81.9,41.6,22.5,61.3,71.5,41.8
1979,29.63336549,27.77047744,63.2,42.06555109,33.62163381,52.3,30.2,73.82114234,9.4,65.08838972,74.2,82.3,42.3,23.7,63.3,73.3,43.6
1980,30.75938956,28.08038075,63.4,43.99925716,36.76572529,54.7,32.5,74.98103152,10.3,65.28413007,74.1,83.5,42.8,24.6,65.1,74.6,44.2
1981,31.31865519,29.84169408,63.3,45.24951206,39.26622984,56.4,34.8,75.84512345,11.6,65.83832154,73.9,84.1,43.2,25.7,66.9,74.7,44.6
1982,32.63666364,34.81624758,63.1,45.96733794,41.94937335,58,36.3,75.84364914,12.4,65.84735212,72.7,84.4,44,27.3,67.5,76.8,44.6
1983,31.6353471,35.82625735,62.4,46.71313451,43.54206966,58.6,37.1,75.95060123,13.1,65.91837999,71.8,84.6,44.3,27.6,67.9,76.1,44.1
1984,31.09294748,35.45308311,62.1,47.66908276,45.12403027,59.1,36.8,75.86911601,13.5,65.74986233,72.1,85.1,46.2,28,68.2,75.9,44.1
1985,31.3796588,36.13334795,61.8,47.9098841,45.747782,59,35.7,75.92343971,13.5,65.79819852,70.8,85.3,46.5,27.5,69,75,43.8
1986,31.19871923,37.24022346,62.1,48.30067763,46.53291505,60,34.7,76.14301516,13.9,65.98256091,71.2,85.7,46.7,28.4,69,75.7,44
1987,31.48642948,38.73067535,61.7,50.20987789,46.69046648,60.2,32.4,76.96309168,14,66.70603055,72,85.5,46.5,30.4,70.1,76.4,43.9
1988,31.08508746,39.3989071,61.7,50.09981147,46.7648277,60.4,30.8,77.62766177,13.9,67.14449816,72.3,85.2,46.2,29.7,70.9,75.6,44.4
1989,31.6124031,39.09653994,62,50.77471585,46.7815648,60.5,29.9,78.11191872,14.1,67.01707156,72.4,84.6,46.2,31.3,71.6,76,44.2
1990,32.70344407,40.82404662,62.6,50.81809432,47.20085084,60.8,29.4,78.86685859,14.1,66.92190193,71.2,83.9,47.3,31.6,72.6,77.6,45.1
1991,34.71183749,33.67988118,62.1,51.46880537,47.22432481,60.8,28.7,78.99124597,14,66.24147465,71.1,83.5,47,32.6,73.2,78.2,45.5
1992,33.93165961,35.20235628,61,51.34974154,47.21939541,59.7,28.2,78.43518191,14.5,65.62245655,71,83,47.4,32.6,73.2,77.3,45.8
1993,34.94683208,35.77715877,60.2,51.12484404,47.63933161,58.7,28.5,77.26731199,14.9,65.73095014,70,82.4,46.4,33.6,73.1,78,46.1
1994,36.03267447,34.43353129,59.4,52.2462176,47.98392441,58.1,28.5,75.81493264,15.7,65.64197772,69.1,81.8,47,34.8,72.9,78.8,46.8
1995,36.84480747,36.06321839,59.2,52.59940342,48.57318101,58.8,27.5,75.12525621,16.2,65.93694921,69.6,81.5,46.1,35.9,73,78.8,47.9
1996,38.96977475,35.9264854,58.6,53.78988011,48.6473926,58.7,27.1,75.03519921,16.7,66.43777883,69.7,81.3,46.4,37.3,73.9,79.8,48.7
1997,40.68568483,35.10193413,58.7,54.99946903,48.56105033,60,26.8,75.1637013,17,66.78635548,70,81.9,47,38.3,74.4,81,49.2
1998,41.91240333,37.59854457,59.1,56.35124789,49.2585152,60,27,75.48616027,17.8,67.2554484,70.1,82.1,48.3,39.7,75.1,81.3,50.5
1999,42.88720191,38.63152919,59.2,58.22882288,49.81020815,61.2,28.1,75.83816206,18.6,67.82022113,70.9,83.5,47.8,40.2,76.5,81.1,51.2
2000,45.05776637,40.02358491,59.2,59.38985737,49.80361649,61.9,27.7,76.69214284,18.4,68.36599498,70.9,83.5,48.2,41,77.5,81.1,51.8
2001,45.86601517,40.69028156,59.4,60.71233149,50.27514494,63,27.6,77.37522931,19,68.57852029,71.2,85.1,47,42.2,77.5,80.9,51.7
2002,47.13465821,41.13295053,60.9,61.8951284,50.5523346,63.7,27,78.64424394,18.7,68.82995959,70.5,85.8,45.7,41.1,77.7,81.3,51.5
2003,47.93518721,42.75854266,61.1,62.1694558,50.34559774,64.6,25.1,78.54494815,18.8,68.89448726,70.6,86.5,46,41.7,77.8,81.5,50.9
2004,47.88714025,43.46649345,61.3,61.91458697,49.95089449,64.2,22.2,78.65074774,18.2,68.45473436,70.8,86.5,44.7,42.1,77.8,80.7,50.5
2005,47.67275409,43.10036784,61.4,61.50098432,49.79185139,63.4,20.6,79.06712173,17.9,68.57122114,69.9,86,45.1,41.6,77.5,81.2,50
2006,46.79029957,44.49933107,61.6,60.17284465,49.21091439,63,18.6,78.68630551,16.8,68.29759443,69.6,85.9,44.1,40.8,77.4,81.2,49.8
2007,47.60502633,43.10045895,61.4,59.41199314,49.00045935,62.5,17.6,78.72141311,16.8,67.87492278,70.2,85.4,44.1,40.7,77.1,82.1,49.3
2008,47.570834,42.71173041,60.7,59.30576517,48.88802678,62.4,17.8,79.19632674,16.5,67.59402834,70.2,85.2,43.3,40.7,77.2,81.7,49.4
2009,48.66722357,43.34892051,61,58.48958333,48.84047414,62.8,18.1,79.5329087,16.8,67.96979204,69.3,85.1,43.3,40.7,77.1,82,49.4
2010,48.73004227,42.06672091,61.3,59.01025521,48.75798769,62.5,17.6,79.61862451,17.2,67.92810557,69,85,43.1,40.2,77,81.7,49.3
2011,50.03718193,42.7734375,61.2,58.7423969,48.18041792,62.2,18.2,79.43281184,17.5,68.42673015,69.5,84.8,43.1,40.1,76.7,81.9,49.2
35 changes: 35 additions & 0 deletions lib/matplotlib/mpl-data/stylelib/clean.mplstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Author: Randal S. Olson (randalolson.com / @randal_olson)
# Produces a clean plotting template with default matplotlib colors.

figure.edgecolor: white
figure.facecolor: white

lines.linewidth: 2.5
lines.markeredgewidth: 0
lines.markersize: 10
lines.dash_capstyle: butt

legend.fancybox: True

font.size: 14

axes.linewidth: 0
axes.titlesize: 22
axes.labelsize: 16

xtick.labelsize: 14
ytick.labelsize: 14
xtick.major.size: 0
xtick.minor.size: 0
ytick.major.size: 0
ytick.minor.size: 0

axes.grid: True
grid.alpha: 0.3
grid.linewidth: 0.5
grid.linestyle: --
grid.color: black

savefig.transparent: False
savefig.bbox: tight
savefig.format: png
4 changes: 4 additions & 0 deletions lib/matplotlib/mpl-data/stylelib/colorblind_10colors.mplstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Author: Randal S. Olson (randalolson.com / @randal_olson)
# Uses Tableau's Color Blind 10 color scheme

axes.color_cycle: 006ba4, ff800e, ababab, 595959, 5f9ed1, c85200, 898989, a2c8ec, ffbc79, cfcfcf
4 changes: 4 additions & 0 deletions lib/matplotlib/mpl-data/stylelib/tableau_10colors.mplstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Author: Randal S. Olson (randalolson.com / @randal_olson)
# Uses Tableau's Tableau 10 color scheme

axes.color_cycle: 1f77b4, ff7f0e, 2ca02c, d62728, 9467bd, 8c564b, e377c2, 7f7f7f, bcbd22, 17becf
4 changes: 4 additions & 0 deletions lib/matplotlib/mpl-data/stylelib/tableau_20colors.mplstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Author: Randal S. Olson (randalolson.com / @randal_olson)
# Uses Tableau's Tableau 20 color scheme

axes.color_cycle: 1f77b4, aec7e8, ff7f0e, ffbb78, 2ca02c, 98df8a, d62728, ff9896, 9467bd, c5b0d5, 8c564b, c49c94, e377c2, f7b6d2, 7f7f7f, c7c7c7, bcbd22, dbdb8d, 17becf, 9edae5