不确定您所说的“组合”是什么意思,但这里有几个可能的解决方案。
样本数据:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 1, 6, 3, 7, 9, 2, 12,5, 3]
s = ['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'E', 'F']
1 - 如果您只想显示更少的刻度,请dtick
在内部使用xaxis
(https://plot.ly/python/reference/#XAxis)
这是默认视图:
>> py.iplot([Bar(x=x, y=y)])

在这里,使用自定义dtick
:
iplot({
'data': [
Bar(x=x, y=y)
],
'layout': {
'xaxis': {
'dtick': 3
}
}
})

2 - 如果您希望您的项目按频率分类,请使用直方图(更多直方图示例)
py.iplot([Histogram(y=y)])

3 - 如果您希望将您的项目合并和汇总,请考虑使用 Pandas:
>> import pandas as pd
>> df = pd.DataFrame({'x': x, 'y': y, 's': ['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'E', 'F']})
>> df
s x y
0 A 1 2
1 B 2 1
2 C 3 6
3 D 4 3
4 A 5 7
5 B 6 9
6 C 7 2
7 D 8 12
8 E 9 5
9 F 10 3
使用groupby
andsum
对属于同一类别的所有项目进行分类和汇总
>> s = df.groupby('s').sum()
>> s
x y
s
A 6 9
B 8 10
C 10 8
D 12 15
E 9 5
F 10 3
>> py.iplot([Bar(x=s.index, y=s.y)])

如果您的数据是数字的,并且您想要对这些值进行分箱和求和,则自己创建数字分箱并绘制结果:
>> import numpy as np
>> bins = np.linspace(df.x.min(), df.x.max(), 5)
>> print bins
[ 1. 3.25 5.5 7.75 10. ]
>> df['groups'] = np.digitize(df.x, bins)
>> print df
s x y groups
0 A 1 2 1
1 B 2 1 1
2 C 3 6 1
3 D 4 3 2
4 A 5 7 2
5 B 6 9 3
6 C 7 2 3
7 D 8 12 4
8 E 9 5 4
9 F 10 3 5
>> grouped = df.groupby('groups').sum()
>> print grouped
x y
groups
1 6 9
2 9 10
3 13 11
4 17 17
5 10 3
>> bin_boundaries = [_ - 0.5 for _ in range(len(grouped.y))]
>> py.iplot({
'data': [Bar(y=grouped.y)],
'layout': {
'bargap': 0,
'xaxis': {
'ticktext': bins,
'tickvals': bin_boundaries
}
}
}, validate=False)
