有没有办法在 matplotlib 上标记/注释我的气泡图(带有 z 轴的散点图)?
数据挖掘
Python
可视化
matplotlib
2021-10-09 02:19:37
1个回答
您可以使用 seaborn 包,使用散点图标记大小来生成您的气泡。然后,您需要遍历数据点并为散点图中的每个点添加一个文本标签。
# Load libraries
import pandas as pd
import matplotlib.pylab as plt
import seaborn as sns
# Create example dataframe
df = pd.DataFrame({
'x': [1, 1.1, 1.2, 2, 5],
'y': [5, 15, 7, 10, 2],
's': [10000,20000,30000,40000,50000],
'group': ['Stamford','Yale','Harvard','MIT','Cambridge']
})
#Create figure
plt.figure(figsize = (15,10))
# Create scatterplot. alpha controls the opacity and s controls the size.
ax = sns.scatterplot(df.x, df.y, alpha = 0.5,s = df.s)
ax.set_xlim(0,6)
ax.set_ylim(-2, 18)
#For each point, we add a text inside the bubble
for line in range(0,df.shape[0]):
ax.text(df.x[line], df.y[line], df.group[line], horizontalalignment='center', size='medium', color='black', weight='semibold')
其它你可能感兴趣的问题

