为了账号安全,请及时绑定邮箱和手机立即绑定

Python Matplotlib - 如何仅在线图上注释 y 轴值?

Python Matplotlib - 如何仅在线图上注释 y 轴值?

慕沐林林 2024-01-04 17:20:19
我在一个图上绘制了 2 个线图,代表 2019 年和 2020 年船舶到达数据。对于每个折线图,y 轴代表船舶到达数量x 轴代表月份(三月、四月、五月等..)但是,我不确定如何让 y 轴值(船舶到达数量)显示在两个折线图上。有人知道怎么做吗?我的代码和折线图如下:# Trendline comparison of the total number of vessel arrivals in 2019 & 2020# Displaying all numbers to 0 decimal placespd.options.display.float_format = "{:,.0f}".formatVESSEL_ARRIVALS_2019 = MARITIME_DATASET_2019["VESSEL ARRIVAL NUMBERS"]VESSEL_ARRIVALS_2020 = MARITIME_DATASET_2020["VESSEL ARRIVAL NUMBERS"]# Displaying the plotted figurefig = plt.figure(figsize = (48, 20))plt.xticks(fontsize = 20)plt.yticks(fontsize = 20)plt.plot(LIST_OF_MONTHS, VESSEL_ARRIVALS_2019, color = "blue", linewidth = 3,  label = "2019 NUMBER OF VESSEL ARRIVALS")plt.plot(LIST_OF_MONTHS, VESSEL_ARRIVALS_2020, color = "red", linewidth = 3,  label = "2020 NUMBER OF VESSEL ARRIVALS")plt.title("TOTAL NUMBER OF VESSEL ARRIVALS 2020 ON 2019 COMPARISON", fontsize = 32)plt.legend(loc = "best", prop = {"size": 26})plt.show()
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

使用该annotate功能。例如,像这样。

offset = 100 # adjust so it looks nice, but isn't on your line

for x, y in zip(LIST_OF_MONTHS, VESSEL_ARRIVALS_2019):

  plt.annotate(y, (x,y+offset))


查看完整回答
反对 回复 2024-01-04
  • 1 回答
  • 0 关注
  • 47 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信