【问题标题】:Mapping Column Data to Graph Properties将列数据映射到图形属性
【发布时间】:2015-03-02 22:02:57
【问题描述】:

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

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

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

我试过了:

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()

但是,我的情节上没有出现任何内容。非常感谢任何帮助。

【问题讨论】:

  • 这是您正在运行的代码吗? ax 来自哪里?
  • 抱歉,我对此很陌生。我已经编辑了我的帖子并包含了我的所有代码。
  • 为什么不直接使用 Pandas 库中的绘图功能?
  • 您能稍微扩展一下吗?

标签: python matplotlib dataframe


【解决方案1】:

这应该可以帮助您入门。 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 http://stackoverflow.com/q/5147112/553404

for idx, row in df.iterrows():
    # see http://stackoverflow.com/q/5147112/553404
    # for better annotation options
    plt.annotate(row['Qname'], xy=(row['X'], row['Y']))

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-13
    • 2017-03-10
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多