5

My question is fairly simple: I would like to visualize multiple histograms using the Seaborn module, however, as a number of bins contain very few counts, I would like to visualize the vertical axis using a logarithmic scale.

My code so far:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(100,2), columns=['A','B'])
df = pd.melt(df, var_name='Category')     
g = sns.FacetGrid(df, col='Category', sharex=True, sharey=False, aspect=1.5)
g = g.map(plt.hist, "value", color="r")

, which gives me the following image: enter image description here

How do I change the vertical axis to a logarithmic scale (in the most 'pythonic'/'seabornic' way)? I've looked around on various answers, but wasn't satisfied with the answers I found so far.

Update: Adding the following code, following the answer here, makes my bars vanish:

g.fig.get_axes()[0].set_yscale('log')

Update II: The following code fixed my problem:

df = pd.DataFrame(np.random.rand(100,2), columns=['A','B'])
df = pd.melt(df, var_name='Category')     
g = sns.FacetGrid(df, col='Category', sharex=True, sharey=False, aspect=1.5)
g = g.map(plt.hist, "value", color="r", log=True)
Archie
  • 2,247
  • 1
  • 18
  • 35

1 Answers1

6

I just added the last couple of lines:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(100,2), columns=['A','B'])
df = pd.melt(df, var_name='Category')  

g = sns.FacetGrid(df, col='Category', col_wrap=2, sharex=True, sharey=False, aspect=1.5)
g = g.map(plt.hist, "value", color="r")
g.axes[0].set_yscale('log')
g.axes[1].set_yscale('log')

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Alessandro
  • 845
  • 11
  • 21
  • The axes change to a logarithmic scale, but the bars dissappear. Any idea what the reason might be? – Archie Feb 15 '18 at 16:27