Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flush away the default suptitle of boxplot with subplots made by pandas package for python

In the following example, I try to make boxplots of "Emission" vs "Voltage" for four "Power" levels, with each power level occupying a subplot.

fig = plt.figure(figsize=(16,9))
i = 0
for Power in [10, 20, 40, 60]:
    i = i+1
    ax = fig.add_subplot(2,2,i)
    subdf = df[df.Power==Power]
    bp = subdf.boxplot(column='Emission', by='Voltage', ax=ax)
fig.suptitle('My Own Title')

The problem is that the

fig.suptitle('My Own Title')

command doesn't flush away the default "Grouped by Voltage" suptitle. What am I missing here? Or is it a bug?

Thanks.

like image 406
Tian He Avatar asked Dec 26 '22 05:12

Tian He


2 Answers

Those are generated by suptitle() calls, and the super titles are the children of fig object (and yes, the suptitle() were called 4 times, one from each subplot).

To fix it:

df = pd.DataFrame({'Emission': np.random.random(12),
                   'Voltage': np.random.random(12),
                   'Power': np.repeat([10,20,40,60],3)})
fig = plt.figure(figsize=(16,9))
i = 0
for Power in [10, 20, 40, 60]:
    i = i+1
    ax = fig.add_subplot(2,2,i)
    subdf = df[df.Power==Power]
    bp = subdf.boxplot(column='Emission', by='Voltage', ax=ax)
fig.texts = [] #flush the old super titles
plt.suptitle('Some title')

enter image description here

like image 137
CT Zhu Avatar answered Dec 31 '22 12:12

CT Zhu


You can also do this without manually creating a figure first:

ax = df.boxplot(by=["some_column"])
ax.get_figure().suptitle("")
like image 26
user3911479 Avatar answered Dec 31 '22 12:12

user3911479