我使用以下代码绘制了data:
fig, (ax) = plt.subplots(1,1, figsize=(12,8))
ax.scatter(x=df['GDP(PPP) per capita'],
y=df['Energy use'],
s=df['Population']/100000,
alpha=0.4,
c=np.arange(132),
cmap='tab10',
edgecolors="gray",
linewidth=0.2)
# Bubble labels
x,y = df['GDP(PPP) per capita'], df['Energy use']
for i, txt in enumerate(df['Country Name']):
plt.annotate(txt, (x[i], y[i]))
print(i, txt, x[i], y[i], df['Population'][i], df['Bubble color'][i])
plt.show()
如您所见,标签重叠且难以阅读。
我想限制要显示的标签,即仅从数据框中的“国家/地区名称”列中选择的国家/地区。怎么做?
谢谢。
答案 0 :(得分:3)
这是一种过滤Country Name
值的方法
chosen_countries = ['Norway', 'Canada'] # fill in the list with countries of your choice
x,y = df['GDP(PPP) per capita'], df['Energy use']
for i, txt in enumerate(df['Country Name']):
if txt in chosen_countries:
plt.annotate(txt, (x[i], y[i]))
print(i, txt, x[i], y[i], df['Population'][i], df['Bubble color'][i])
plt.show()