Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 206
user11994 Avatar asked Aug 22 '14 16:08

user11994


People also ask

Do I still need %Matplotlib inline?

Note that in current versions of JupyterLab and Jupyter Notebook, the explicit use of the %matplotlib inline directive is not needed anymore, though other third-party clients may still require it.

How do you display plots in Jupyter notebook?

Jupyter Notebook - Big Data Visualization Tool IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called.

Why use Seaborn instead of matplotlib?

Seaborn is more comfortable in handling Pandas data frames. It uses basic sets of methods to provide beautiful graphics in python. Matplotlib works efficiently with data frames and arrays.It treats figures and axes as objects. It contains various stateful APIs for plotting.


3 Answers

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)
like image 87
mwaskom Avatar answered Oct 21 '22 19:10

mwaskom


sns.set_style("darkgrid")

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

enter image description here

like image 42
The Unfun Cat Avatar answered Oct 21 '22 18:10

The Unfun Cat


Why not simply

plt.style.use("dark_background")

like image 43
crypdick Avatar answered Oct 21 '22 19:10

crypdick