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

时间:2020-10-25 14:30:53

标签: python matplotlib linegraph yaxis

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

对于每个折线图,
y轴表示船只到达的编号
x轴表示月份(3月,4月,5月等)。

但是,我不确定如何使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()

enter image description here

1 个答案:

答案 0 :(得分:1)

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