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()
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
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()