Last active
November 23, 2015 17:31
-
-
Save davidwessman/d2d142fc593fde489d46 to your computer and use it in GitHub Desktop.
Black and white plots in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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