Set no title for pandas boxplot (groupby)
Asked Answered
T

8

45

When drawing a pandas boxplot, grouped by another column, pandas automatically adds a title to the plot, saying 'Boxplot grouped by....'. Is there a way to remove that? I tried using

suptitle('')

as per Pandas: boxplot of one column based on another column

but this does not seem to work. I am using latest pandas (0.13.1) version.

Transgress answered 7/5, 2014 at 1:30 Comment(0)
T
43

Make sure your calling suptitle('') on the right figure.

In [23]: axes = df.boxplot(by='g')

In [24]: fig = axes[0][0].get_figure()

In [25]: fig.suptitle('')
Out[25]: <matplotlib.text.Text at 0x109496090>
Topliffe answered 7/5, 2014 at 3:31 Comment(4)
I'm getting 'AxesSubplot' object is not subscriptable when I try this. Has something changed since this answer in 2014?Everick
I'm having the same problem. The answer from nick (Jan 20, 2019) worked for me.Zulmazulu
Try: axes.iloc[0].get_figure() If you look at the axes variable, you'll likely see it's a pandas Series. And you need to get any of the several Axes in this Series and then call get_figure on it. By doing iloc[0] you'll get the first element in the SeriesBusybody
It should be axes.get_figure().suptitle("")Looselimbed
E
38

I had the same problem. Ended up using this solution

import matplotlib.pyplot as plt    
# df is your dataframe
df.boxplot(column='value', by='category')
title_boxplot = 'awesome title'
plt.title( title_boxplot )
plt.suptitle('') # that's what you're after
plt.show()
Eada answered 20/1, 2019 at 18:6 Comment(1)
Hi nick. I notice that you're still suggesting edits that just add the dataframe tag to questions. Please don't. I realise that the pandas tag excerpt used to explicitly say that this should be done, but it's been recently updated since that guidance didn't really make sense - see meta.https://mcmap.net/q/374256/-getting-python-to-work-internal-server-error/1709587 for the discussion about this. Not your fault since you were acting in good faith on guidance in the excerpt, but those edits are pointless at best and require reviewer time. Feel free to flag this comment as "no longer needed" once you've read it.Makassar
R
6

In Pandas 1.1.4, the following worked for me:

ax = pft[[col, "abuse_marked"]].boxplot(by="abuse_marked", vert=False)
ax.get_figure().suptitle("")

enter image description here

Rathbun answered 2/9, 2022 at 11:49 Comment(0)
F
5

After trying all the suggestions, only this modification worked for me, which also lets you modify other parameters:

ax = df.boxplot(by ='value', column =['category'], grid = False);
plt.title('')
plt.suptitle('')
ax.set_title('');
ax.set_xlabel("x_label");
ax.set_ylabel("y_label");
ax = plt.show()
Flank answered 10/8, 2021 at 10:59 Comment(0)
D
2

None of the above solutions worked for me, but this one did:

axes = df.boxplot(column=values, by=index, ax=ax, rot=90)
axes.set_title('')

Dagan answered 9/11, 2020 at 13:6 Comment(0)
P
1

I as having problems with this and generally never liked the canned title that the pandas was adding as it was dependent on the column names which are typically never publishing ready.

You can edit the source code in ~\pandas\plotting\_core.py

On line 2698 you will find:

fig.suptitle('Boxplot grouped by {byline}'.format(byline=byline))

Simple comment this line out and pandas will no longer add the title to the top of the boxplot by default. You will have to redo this change as you upgrade pandas versions.

Primary answered 18/10, 2018 at 15:25 Comment(1)
Never change the source library locally. Your results will never be replicable.Vidavidal
R
0

This worked for me:

ax = df.boxplot(column = col1, by = col2)

ax.get_figure().suptitle()

Ricercar answered 16/2, 2022 at 15:3 Comment(0)
I
0

Although the previous answers correctly remove the text in the suptitle, they still leave space for it. This might be an issue in certain cases, such as when using fig.tight_layout(). Therefore, to completely remove the title, this can be done:

axes = df.boxplot(by='g')
fig = axes[0][0].get_figure()
fig._suptitle.set_visible(False)
fig._suptitle.set_in_layout(False)
Isis answered 21/8, 2022 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.