本文介绍了如何更改按Boxplot分组的 pandas 中的组标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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()
我想更改两个标签 Col1
和 Col2
,我尝试传递参数 labels = ['Left label','Right label']
(到matplotlib核心功能https://matplotlib.org/api/_as_gen/matplotlib.pyplot.boxplot.html#matplotlib.pyplot.boxplot ),但运气不好:
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'])
给我错误:
ValueError: Dimensions of labels and X must be compatible
推荐答案
尝试一下,因为boxplot返回一个NumPy轴数组,所以您可以使用此NumPy数组和set_title的每个元素:
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()
这篇关于如何更改按Boxplot分组的 pandas 中的组标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!