Skip to content

Instantly share code, notes, and snippets.

@davidwessman
Last active November 23, 2015 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwessman/d2d142fc593fde489d46 to your computer and use it in GitHub Desktop.
Save davidwessman/d2d142fc593fde489d46 to your computer and use it in GitHub Desktop.
Black and white plots in Python.
def setAxLinesBW(ax):
"""
Take each Line2D in the axes, ax, and convert the line style to be
suitable for black and white viewing.
"""
MARKERSIZE = 3
COLORMAP = {
'b': {'marker': None, 'dash': (None,None)},
'g': {'marker': None, 'dash': [5,5]},
'r': {'marker': None, 'dash': [5,3,1,3]},
'c': {'marker': None, 'dash': [1,3]},
'm': {'marker': None, 'dash': [5,2,5,2,5,10]},
'y': {'marker': None, 'dash': [5,3,1,2,1,10]},
'k': {'marker': 'o', 'dash': (None,None)} #[1,2,1,10]}
}
lines = []
if ax.get_legend() == None:
lines = ax.get_lines()
else:
lines = ax.get_lines() + ax.get_legend().get_lines()
for line in lines:
origColor = line.get_color()
line.set_color('black')
line.set_dashes(COLORMAP[origColor]['dash'])
line.set_marker(COLORMAP[origColor]['marker'])
line.set_markersize(MARKERSIZE)
def setFigLinesBW(fig):
"""
Take each axes in the figure, and for each line in the axes, make the
line viewable in black and white.
"""
for ax in fig.get_axes():
setAxLinesBW(ax)
import matplotlib.pyplot as plt
import black_and_white
# Some data...
fig = plt.figure()
plt.plot(x,y)
plt.legend(['First', 'Second', Third'], loc="upper left")
plt.title...
bw.setFigLinesBW(fig)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment