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

Time:10-13

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)

The histogram looks like this:

actual_output

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

expected_ouput

Note: there is an error when uploading images to stackoverflow at the moment. Will do so when it is back, sorry.

Failed to upload image; an error occurred on the server

CodePudding user response:

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

CodePudding user response:

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()
  • Related