我已经尽可能地重写了代码。主要问题是您生成了两个轴对象——一个带有for xe, ye,c in zip(x, y,colors): plt.scatter([xe] * len(ye), ye, c=c),另一个带有plt.axes().set_xticklabels(['Part 1','Part 2'],rotation = 45)。您可以直接使用plt.xticks() 提供标签:
import matplotlib.pyplot as plt
x = [1,2]
y = [[0.1, 0.6, 0.9],[0.5,0.7,0.8]]
colors = ['red', 'blue']
plt.title("Algorithm comparison - p-values")
for xe, ye,c in zip(x, y,colors):
plt.scatter([xe] * len(ye), ye, c=c)
plt.xticks([1,2], ['Part 1','Part 2'], rotation = 45)
plt.show()
最好在开始时创建一个轴对象并使用此轴对象绘制所有内容:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1,2]
y = [[0.1, 0.6, 0.9],[0.5,0.7,0.8]]
colors = ['red', 'blue']
ax.set_title("Algorithm comparison - p-values")
for xe, ye, c in zip(x, y, colors):
ax.scatter([xe] * len(ye), ye, c=c)
ax.set_xticks(x)
ax.set_xticklabels(['Part 1','Part 2'],rotation = 45)
ax.set_xlim(0.5, 2.5)
plt.show()
示例输出:
有关面向对象方法和 plt 接口之间差异的更多说明,请参见 here 和 the matplotlib documentation。