using scientific notation kdeplot

Question:

I’m trying to create a kde plot using seaborn in python but when setting the colorbar values to show in scientific notation I see no difference.

See – making colorbar with scientific notation in seaborn for a heavily related topic.

See – https://seaborn.pydata.org/generated/seaborn.kdeplot.html for the documentation of seaborn’s kde class.

Is there some reason why this doesn’t work for the kde class or am I making a silly mistake in my formatting?

import numpy as np
import seaborn as sns
import matplotlib.ticker as tkr

a = np.random.normal(0,1,size=100)
b = np.random.normal(0,1,size=100)

fig, ax = plt.figure(), plt.subplot(111)
formatter = tkr.ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
sns.kdeplot(a,b, n_levels=10, shade=True, cmap='Blues',
            cbar=True, cbar_kws={'format':formatter})

The result:

enter image description here

Here I would be expecting the colourbar to show an indexed notation as in the first link in the description of this problem.

Asked By: user8188120

||

Answers:

.set_scientific(True) applies to the offset label. Here you don’t have any offset, so it seems like it’s ignored. There is unfortunately no canonical way to format the ticklabels themselves in scientific notation.

One method is shown in Show decimal places and scientific notation on the axis

Applying it here:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

a = np.random.normal(0,1,size=100)
b = np.random.normal(0,1,size=100)

fig, ax = plt.figure(), plt.subplot(111)

f = mticker.ScalarFormatter(useOffset=False, useMathText=True)
g = lambda x,pos : "${}$".format(f._formatSciNotation('%1.10e' % x))

sns.kdeplot(a,b, n_levels=10, shade=True, cmap='Blues',
            cbar=True, cbar_kws={'format': mticker.FuncFormatter(g)})

plt.show()

enter image description here