微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于散点图可视化python设置值

如何解决基于散点图可视化python设置值

如果变量位于散点图中的某个部分,是否可以为变量设置标签?还是只能手动?

每个彩色点都是可变的。

that's what I get with help of a scatter plot

因此,我想用一些 python 脚本来获得它,但我不知道如何:

var   | label
-------------
3 65  | low
3 40  | low
7 180 | high
     ...
5 40  | undervalued

解决方法

我不知道有任何 matplotlib 函数可以满足您的需求,但是您可以使用 annotate 轻松注释您的观点:

import matplotlib.pyplot as plt

# So starting from your example
y = [65,40,180,40]
x = [3,3,7,5]
labels = ['low','low','high','undervalued']

fig,ax = plt.subplots()
ax.scatter(x,y)

for i,label in enumerate(labels):
    ax.annotate(label,(x[i],y[i]))

或者,如果每个标签都有一些间隔,您可以编写一个返回所需标签的函数,例如:

import matplotlib.pyplot as plt

# Based on the image you provided
def get_label(x,y):
    if x<4 and y<100:
        return 'low'
    elif x<4 and y>=100:
        return 'overvalued'
    elif x>=4 and y<100:
        return 'undervalued'
    else:
        return 'high'

y = [65,5]

fig,y)

for x_current,y_current in zip(x,y):
    ax.annotate(get_label(x_current,y_current),(x_current,y_current))

您可以在 documentation 中找到更多格式。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。