繁体   English   中英

Python Matplotlib - 如何仅在第 plot 行注释 y 轴值?

[英]Python Matplotlib - How to annotate y-axis values only on line plot?

我在一个 plot 上绘制了 2 个折线图,代表 2019 年和 2020 年船只到达的数据。

对于每个折线图,
y 轴表示船只到达数量
x 轴代表月份(三月、四月、五月等)

但是,我不确定如何让 y 轴值(船只到达编号)出现在两个折线图上。

有谁知道怎么做?

我的代码和折线图如下所示:

# Trendline comparison of the total number of vessel arrivals in 2019 & 2020

# Displaying all numbers to 0 decimal places
pd.options.display.float_format = "{:,.0f}".format

VESSEL_ARRIVALS_2019 = MARITIME_DATASET_2019["VESSEL ARRIVAL NUMBERS"]
VESSEL_ARRIVALS_2020 = MARITIME_DATASET_2020["VESSEL ARRIVAL NUMBERS"]

# Displaying the plotted figure
fig = 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()

在此处输入图像描述

使用annotate function。例如,像这样。

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM