我计算这些值的平均值并绘制出来。我希望平均值的颜色与线条相同。我使用的代码如下:
with open('path_to_json/values.json', 'r') as outfile:
a = json.load(outfile)
sum = 0
for k, v in a.items():
sum += v
avg = sum / len(a.values())
fig, ax = plt.subplots(1,1)
p=ax.plot(list(a.keys()),list(a.values()))
p=ax.plot([0, len(a.values())], [avg, avg], "--")
yt = ax.get_yticks()
yt = np.append(yt, avg)
xticks = np.arange(0, len(a.values()), 10)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
ax.set_xticks(xticks)
ax.set_yticks(yt)
plt.show()