Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a pandas hist () plot with sub-histograms, how to insert titles for the x and y axes, and overall title?

I am using the pandas hist() method with the 'by' option, specifically:

histos=data_ok._DiffPricePercent.hist(by=input_data._Category, sharex=True, sharey=True )

This command produces this plot: This is the plot I am getting

How do I add titles for respectively the x and y axes on each of the sub-histograms, or alternatively, overall? Also, how to insert an overall title for the plot?

I tried the following, but it does not go through (with error "AttributeError: 'numpy.ndarray' object has no attribute 'set_ylabel'"):

histos.set_ylabel('Fréquence')
histos.set_xlabel('Variation prix suggérée, en %')

Many thanks in advance for any suggestions

like image 254
Charles Avatar asked Nov 22 '25 00:11

Charles


1 Answers

What you actually get is an object-type numpy array with elements in it that are the AxesSubplot instances. Try

histos[0].set_xlabel('My x label 1')
histos[1].set_xlabel('My x label 2')

EDIT : To change the format of ticks, use a Formatter:

from matplotlib.ticker import FormatStrFormatter

maj_frm = FormatStrFormatter('%.1f')
...
histos[0].xaxis.set_major_formatter(maj_frm)

The documentation on tick locating and formatting can be found here.

like image 160
Ajean Avatar answered Nov 23 '25 14:11

Ajean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!