Python Matplotlib: 绘制两条折线图并标注数据
以下是使用 Python Matplotlib 库绘制有两条折线的折线图,并在图上标注数据的示例代码:
import matplotlib.pyplot as plt
# 数据
x = range(1, 6)
y1 = [0.5, 0.7, 1.2, 1.8, 2.5]
y2 = [0.8, 1.1, 1.5, 2.0, 2.7]
# 绘图
plt.plot(x, y1, '-o', label='line1')
plt.plot(x, y2, '-o', label='line2')
# 标注数据
for i, j in zip(x, y1):
plt.text(i, j, str(j))
for i, j in zip(x, y2):
plt.text(i, j, str(j))
# 设置图例、标题等
plt.legend()
plt.title('Two lines chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# 显示图形
plt.show()
运行上述代码,即可绘制出如下的折线图:

原文地址: https://www.cveoy.top/t/topic/mHVe 著作权归作者所有。请勿转载和采集!