我有一些国家/地区,我正在比较人均国内生产总值和地理纬度。 我在这里开发了代码:
import matplotlib.pyplot as plt
plt.rc('font', size=12)
plt.rc('axes', labelsize=14, titlesize = 14)
plt.rc('legend', fontsize =12)
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
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"
min_gdp_ppp = 25
max_gdp_ppp = 125_000
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', width=3,
shrink=0.08, headwidth=5))
plt.plot(pos_data_x, pos_data_y, "ro")
plt.axis([min_lat, max_lat, min_gdp_ppp, max_gdp_ppp])
plt.show()
结果如下图所示:
然而我的箭上没有茎。 我尝试更改箭头图参数但没有成功。
我正在尝试创建看起来更像这样的东西:
来自:https://www.analyticsvidhya.com/blog/2024/02/scatter-plot-visualization-in-python-using-matplotlib/
您应该尝试将
arrowstyle='->'
添加到 arrowprops=dict(facecolor='black', width=3, shrink=0.08, headwidth=5)
。你应该有这样的东西:
plt.annotate(
country,
xy=(pos_data_x, pos_data_y),
xytext=pos_text,
fontsize=12,
arrowprops=dict(facecolor='black', width=3, shrink=0.08, headwidth=5, arrowstyle='->')
)