The Python Oracle

Setting axis labels for histogram pandas

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Jungle Looping

--

Chapters
00:00 Setting Axis Labels For Histogram Pandas
00:37 Accepted Answer Score 38
00:59 Answer 2 Score 1
01:12 Answer 3 Score 0
01:25 Thank you

--

Full question
https://stackoverflow.com/questions/4283...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #matplotlib

#avk47



ACCEPTED ANSWER

Score 38


Labels are properties of axes objects, that needs to be set on each of them. Here's an example that worked for me:

frame = pd.DataFrame([np.random.rand(20), np.sign(np.random.rand(20) - 0.5)]).T
frame.columns = ['Age', 'Survived']

# Note that you can let the hist function do the groupby
# the function hist returns the list of axes created
axarr = frame.hist(column='Age', by = 'Survived', sharex=True, sharey=True, layout = (2, 1))

for ax in axarr.flatten():
    ax.set_xlabel("Age")
    ax.set_ylabel("Individuals")



ANSWER 2

Score 1


I came here looking for the same thing, but found it easier to do in plotly

import plotly.express as px

px.histogram(df, x="t_depth", nbins=150).update_layout(
    xaxis = dict(dtick=10), bargap=0.2
)

enter image description here




ANSWER 3

Score 0


For those looking for a simple one line solution in matplotlib, just tack on .set_xlabel("your_label") to the end.

df.col_to_graph.plot.hist(bins=10, alpha=0.5, figsize=(16, 12)).set_xlabel("col_to_graph label")