我有一些国家/地区,我正在比较人均国内生产总值和地理纬度。我在这里开发了代码:
country_sub.plot(kind='scatter', figsize=(6, 4), grid=True, x=lat_col, y=gdp_ppp_col)
gdp_ppp_col = "gdp_ppp"
lat_col = "lat"
position_text = {
"Israel": (31.04605, 57_758),
"United States": (37.09024, 75_269),
"Spain": (40.463667, 29_385),
"Bulgaria": (42.733883, 13_129),
"Germany": (51.165691, 48_845),
"Ireland": (53.41291, 105_362),
"Iceland": (64.963051, 74_663)
}
for country, pos_text in position_text.items():
pos_data_x = country_sub[lat_col].loc[country]
pos_data_y = country_sub[gdp_ppp_col].loc[country]
country = "U.S." if country == "United States" else country
plt.annotate(country,
xy=(pos_data_x, pos_data_y),
xytext=pos_text,
fontsize=12,
arrowprops=dict(facecolor='black', arrowstyle='->')
)
plt.plot(pos_data_x, pos_data_y, "ro")
plt.axis([25, 70, -5000, 125000])
plt.show()
然而,这会产生下图:
有没有办法将国家/地区注释移动到箭头的后端?
尝试添加到
xytext
:
...
plt.annotate(country,
xy=(pos_data_x, pos_data_y),
xytext=(pos_text[0] + 10, pos_text[1] + 10),
fontsize=12,
arrowprops=dict(facecolor='black', arrowstyle='->')
)
...