fabda01
fabda01

Reputation: 3753

How to remove y-axis tick labels from pandas DataFrame histogram

I want to plot a histogram of my DataFrame using pandas.DataFrame.hist, but I don't want to show the y-axis tick labels.

I have tried this solution and this solution, but it still doesn't work for pandas.DataFrame.hist

So far the code looks like this

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'length': [1.5, 0.5, 1.2, 0.9, 3],
    'width': [0.7, 0.2, 0.15, 0.2, 1.1]
    }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])

fig, ax = plt.subplots()
ax.axes.get_yaxis().set_visible(False)
hist = df.hist(bins=3, ax=ax)

And the histogram looks like this:

enter image description here

But I want it to look like this (edited on mspaint):

enter image description here

Upvotes: 3

Views: 673

Answers (2)

AlexWach
AlexWach

Reputation: 602

i guess the correct way to access the AxesSubplot objects would be through the hist object, like this. produces the desired output (again upload of figs currently not possible)

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'length': [1.5, 0.5, 1.2, 0.9, 3],
    'width': [0.7, 0.2, 0.15, 0.2, 1.1]
    }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])

fig, ax = plt.subplots()
hist = df.hist(bins=3, ax=ax)
hist[0][0].get_yaxis().set_visible(False)
hist[0][1].get_yaxis().set_visible(False)
plt.show()

Upvotes: 1

tturbo
tturbo

Reputation: 1128

Very strange, i do not know why your code does not work. However i found a workaround:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'length': [1.5, 0.5, 1.2, 0.9, 3],
    'width': [0.7, 0.2, 0.15, 0.2, 1.1]
    }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])

fig, ax = plt.subplots()
df.hist(bins=3, ax=ax, ylabelsize=0) # <- set ylabelsize to zero

Upvotes: 3

Related Questions