Seaborn: How use a log scale with a kdeplot?

Created on 11 Oct 2015  路  5Comments  路  Source: mwaskom/seaborn

import numpy as np, seaborn as sns, matplotlib.pyplot as plt
np.random.seed(1)
data = np.power(np.random.randn(1000), 10)
sns.kdeplot(data, shade=True)
plt.xscale('log')
plt.show()

looks pretty atrocious

test

is the recommend solution just taking the log of data prior to plotting and then fixing the ticks? Or is there a better way?

Most helpful comment

Is the recommend solution just taking the log of data prior to plotting and then fixing the ticks?

Yep; sorry.

All 5 comments

Is the recommend solution just taking the log of data prior to plotting and then fixing the ticks?

Yep; sorry.

How would you recommend to fix the ticks?

ax.set(xticklabels=)

Extracting the labels and transforming them turns out to be a bit complicated, since matplotlib doesn't assign labels before drawing them, and it gives a medium dash for negatives that breaks int conversion.

Here's one way:

import numpy as np, seaborn as sns, matplotlib.pyplot as plt
np.random.seed(1)
data = np.power(np.random.randn(1000), 10)

fig, ax = plt.subplots()
sns.kdeplot(np.log10(data), shade=True, ax=ax)
fig.canvas.draw()
locs, labels = plt.xticks()
# u2212 is the matplotlib's medium dash for negative numbers.
ax.set(xticklabels=[10 ** int(i.get_text().replace(u'\u2212', '-'))
                    for i in labels])
# Or for scientific notation:
# ax.set(xticklabels=["$10^" + i.get_text() + "$" for i in labels])
plt.show()

chart

Closed in #2104 with the log_scale parameter in kdeplot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ConstantinoSchillebeeckx picture ConstantinoSchillebeeckx  路  4Comments

queryous picture queryous  路  4Comments

chanshing picture chanshing  路  3Comments

rrbarbosa picture rrbarbosa  路  3Comments

JanHomann picture JanHomann  路  3Comments