Warm tip: This article is reproduced from stackoverflow.com, please click
boxplot pandas python

How can I change the group titles in a pandas grouped-by boxplot?

发布于 2021-02-08 09:57:09
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.randn(10, 3),
                  columns=['Col1', 'Col2', 'Col3'])
df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
                     'B', 'B', 'B', 'B', 'B'])
df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A',
                     'B', 'A', 'B', 'A', 'B'])
boxplot = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'])

plt.show()

enter image description here

I would like to change the two labels Col1 and Col2, I have tried to pass the argument labels=['Left label','Right label'] (to the matplotlib core function https://matplotlib.org/api/_as_gen/matplotlib.pyplot.boxplot.html#matplotlib.pyplot.boxplot) but with no luck:

boxplot = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'], labels=['Left label','Right label'])

gives me the error:

ValueError: Dimensions of labels and X must be compatible
Questioner
Alessandro Jacopson
Viewed
0
Scott Boston 2020-10-13 22:14

Try this, because boxplot here returns a NumPy array of axes, you can use each element of this NumPy array and set_title:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.randn(10, 3),
                  columns=['Col1', 'Col2', 'Col3'])
df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
                     'B', 'B', 'B', 'B', 'B'])
df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A',
                     'B', 'A', 'B', 'A', 'B'])
ax = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'])

ax[0].set_title('AAA')
ax[1].set_title('BBB')

plt.show()