How to clear default suptitle boxplot with subheadings made by pandas package for python

In the following example, I am trying to make boxes with cells "Radiation" and "Voltage" for four power levels, with each power level occupying a subtitle.

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

fig.suptitle('My Own Title')
Team

does not remove the default "Grouped by voltage". What am I missing here? Or is this a mistake?

Thank.

+4
source share
3 answers

They are generated by calls suptitle(), and super-headers are the children of the object fig(and yes, they suptitle()were called 4 times, one from each subtitle).

To fix this:

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

+5

, :

ax = df.boxplot(by=["some_column"])
ax.get_figure().suptitle("")
+3

So far, someone can give a better answer, here's a hack, assuming the new heading is longer than the old.

fig.suptitle('My Own Title',backgroundcolor='white', color='black')

It basically hides the old heading, rather than flushing it.

0
source

All Articles