Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How Add Intelligent Title to Pandas DataFrame.plot.hist

I have a simple data set of multiple columns, all of which I want to plot against a "quality" score from 1-10. NOTE: this is a group_by plot, not a simple plot of a single column. It yields a series of histogram bars for all the other columns in the dataframe, one histogram for each of the "quality" values in the data. Since in this case the "quality" scores range from 3 to 9, a total of 7 histograms result.

df.plot.hist(by='quality',figsize=(10,30))
plt.tight_layout()
plt.show()

This group_by histogram plot works beautifully with one issue: the top title of each individual histogram defaults to the numeric "quality" value, i.e, "3", "4", "5", etc., but does not name the column from which that numeric "quality" value is given.
example of grouped histogram result
What I would like to do is capture that value but add the column name (or some such string) to it like so:

"Quality: " + str(whatever_the_quality_value_variable_is_called)

Can anyone suggest how to set plt.title for each histogram to something other than a fixed string? I find many pages showing how to set the trivial plt.title("my_title") which is not helpful.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Looking for something like

plt.title("My Useful Title Labeling Variable Value" + {the variable}")

Thank you in advance for spending time and thought on this.

Here is a sample of the data:
sample data

RESOLUTION:
Here is the result of Jesse’s code. Beautiful.
resolution example

>Solution :

Solution:

You can import matplotlib then capture all of the axes that are being generated ‘axes’, then iterate over them and set their titles individually.

import matplotlib.pyplot as plt

axes = df.plot.hist(by='quality',figsize=(10,30))

variable_list = ['varname_1', 'varname_2']

for i,ax in enumerate(axes):

    ax.set_title(f"My Useful Title Labeling Variable Value {variable_list[i]}")

plt.tight_layout()
plt.show())

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading