Best way to display Seaborn/Matplotlib plots with a dark iPython Notebook profile
Asked Answered
S

5

25

I use the fantastic Seaborn library for some summary stats in IPython Notebook. I recently switched over to a dark color theme for my notebooks and am trying to figure out the best seeings for Seaborn with a dark background. I am using the darkgrid style but the legends are still being printed in black which makes them impossible to read. Here is an example:

Seaborn on dark background

What is the best way to fix my styles of use of Seaborn to make the legends appear on white?

UPDATE: I just noticed that the same problem exists with my Matplotlib graphs.. so my question is more general. What styles do you use to allow for white on dark plots so the legends are readable?

Saphra answered 22/8, 2014 at 16:10 Comment(0)
M
26

You can customize the seaborn style, and it tries to make it relatively easy to do so.

If you want to see each of the parameters that is considered part of the "style" definition, just call sns.axes_style() with no arguments, and it will return the current settings. As of 0.3.1 and for the default style ("darkgrid"), that looks like this:

{'axes.axisbelow': True,
 'axes.edgecolor': 'white',
 'axes.facecolor': '#EAEAF2',
 'axes.grid': True,
 'axes.labelcolor': '.15',
 'axes.linewidth': 0,
 'font.family': 'Arial',
 'grid.color': 'white',
 'grid.linestyle': '-',
 'image.cmap': 'Greys',
 'legend.frameon': False,
 'legend.numpoints': 1,
 'legend.scatterpoints': 1,
 'lines.solid_capstyle': 'round',
 'pdf.fonttype': 42,
 'text.color': '.15',
 'xtick.color': '.15',
 'xtick.direction': 'out',
 'xtick.major.size': 0,
 'xtick.minor.size': 0,
 'ytick.color': '.15',
 'ytick.direction': 'out',
 'ytick.major.size': 0,
 'ytick.minor.size': 0}

A good heuristic is that you probably only need the parameters with "color" in the name, so you can filter it:

{k: v for k, v in sns.axes_style().items() if "color" in k}

returns

{'axes.edgecolor': 'white',
 'axes.facecolor': '#EAEAF2',
 'axes.labelcolor': '.15',
 'grid.color': 'white',
 'text.color': '.15',
 'xtick.color': '.15',
 'ytick.color': '.15'}

You can then pass a custom dictionary with values for these parameters into sns.set_style():

custom_style = {'axes.labelcolor': 'white',
                'xtick.color': 'white',
                'ytick.color': 'white'}
sns.set_style("darkgrid", rc=custom_style)
Missionary answered 22/8, 2014 at 16:52 Comment(1)
Yeah, but: can you set it as default? Or do you need to paste this piece of code in every notebook?Decorative
W
14

Why not simply

plt.style.use("dark_background")

Wale answered 6/12, 2018 at 13:20 Comment(1)
This messes with seaborn styles, e.g. with that I get a too large font on some plots such that tick labels overlap.Madelina
W
12
sns.set_style("darkgrid")

Will make the background white so that you can see the text.

enter image description here

Whirly answered 24/11, 2015 at 13:28 Comment(0)
I
5

I find that adding this

plt.figure(facecolor='w')

each time I plot takes care of the axes background.

Inextricable answered 6/3, 2015 at 21:49 Comment(0)
G
0

These 2 lines can be used with all seaborn styles: 'white', 'dark', 'whitegrid', 'darkgrid', 'ticks'

sns.set_style("darkgrid")  # adds seaborn style to charts, eg. grid
plt.style.use("dark_background")  # inverts colors to dark theme
Gust answered 14/2 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.