熊猫:情节中的分组和xticks

时间:2018-10-16 15:06:24

标签: python pandas matplotlib plot

我正在使用具有以下结构的同一个日期:

data

我想按B列对数据进行分组,并获得绘图和比较的平均值。

sub_data = data_composite.groupby(['B']).aggregate(np.mean)
ax = sub_data.plot()

获取:

Plot result

但是,我想从图中得到相应的xticks。它将是KP40,KP08等...类似这样:

enter image description here

有什么办法吗?

非常感谢。问候

2 个答案:

答案 0 :(得分:1)

它应该为您工作

import numpy as np
import matplotlib.pyplot as plt

tmp_labels = data_composite.drop_duplicates(subset='B', keep = 'first')
xlabels = tmp_labels['B'].values
plt.xticks(np.arange(sub_data.shape[0]),list(xlabels), rotation=90)

答案 1 :(得分:0)

我发现使用index列作为plt.xticks的输入可以更轻松地完成此操作。

sub_data = data_composite.groupby(['B']).aggregate(np.mean)
ax = sub_data.plot()

ax.set_xlabel(x)
ax.set_ylabel('Quantity of {}'.format(y)) 

plt.xticks(np.arange(groupped_data.shape[0]),list(groupped_data.index), rotation=90);