3

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

fabda01
  • 3,384
  • 2
  • 31
  • 37
  • *Failed to upload image; an error occurred on the server* same error happened to me in my answer. So no picture there either... – tturbo Oct 13 '22 at 07:34

2 Answers2

3

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
tturbo
  • 680
  • 5
  • 16
1

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()
AlexWach
  • 592
  • 4
  • 16