将列数据映射到图形属性

Mapping Column Data to Graph Properties

我有一个名为 df 的数据框,如下所示:

Qname    X    Y    Magnitude
Bob      5    19    10
Tom      6    20    20
Jim      3    30    30

我想制作数据的可视化文本图。我想在坐标设置为 X,Y 和 s=Size 的图形上绘制 Qnames。

我试过:

fig = plt.figure()
ax = fig.add_axes((0,0,1,1))
X = df.X
Y = df.Y
S = df.magnitude
Name = df.Qname
ax.text(X, Y, Name, size=S, color='red', rotation=0, alpha=1.0, ha='center', va='center')
fig.show()

但是我的情节中什么也没有出现。非常感谢任何帮助。

这应该可以帮助您入门。 Matplotlib 不会为您处理文本放置,因此您可能需要尝试一下。

import pandas as pd
import matplotlib.pyplot as plt

# replace this with your existing code to read the dataframe
df = pd.read_clipboard()

plt.scatter(df.X, df.Y, s=df.Magnitude)


# annotate the plot
# unfortunately you have to iterate over your points
# see 

for idx, row in df.iterrows():
    # see 
    # for better annotation options
    plt.annotate(row['Qname'], xy=(row['X'], row['Y']))

plt.show()