我觉得你想要的更像
x y label
1 12 A
2 13 A
3 15 A
4 29 A
5 23 B
6 23 B
7 23 B
8 90 C
9 83 C
而不是重复标签,您希望每组有一个居中的标签?
from itertools import groupby
import matplotlib.pyplot as plt
data = [
('A', 12),
('A', 13),
('A', 15),
('A', 29),
('B', 23),
('B', 23),
('B', 23),
('C', 90),
('C', 83)
]
labels,ys = zip(*data)
xs = range(len(ys))
def centered(val, ln, empty=''):
half = (ln - 1) // 2
return (val if i==half else empty for i in xrange(ln))
def centered_labels(labels):
return [v for val,grp in groupby(labels) for v in centered(val, len(list(grp)))]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xticklabels(centered_labels(labels))
ax.plot(xs, ys, 'b-', linewidth=3)
结果是