python - 基于散点图可视化python设置值
问题描述
如果变量位于散点图中的某个部分,是否可以为变量设置标签?还是只能手动?
每个彩色点都是可变的。
结果,我想用一些 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, 40, 180, 40]
x = [3, 3, 7, 5]
fig, ax = plt.subplots()
ax.scatter(x, y)
for x_current, y_current in zip(x,y):
ax.annotate(get_label(x_current, y_current), (x_current, y_current))
您可以在文档中找到更多格式。
推荐阅读
- class - 在类中存在的标题内使用“导航”和“路由” - React-navigation v5
- excel - EXCEL FORMULA:在 2 个未知点之间进行插值
- gitlab - gitlab 服务器未启动。我收到这个错误
- java - 从 Whatsapp 企业帐户在我们的企业中发送群组短信
- apache - 设置 apache&nginx 反向代理以显示来自其他用户 public_html 的网站
- python-requests - 如何使用签名秘密和 python 请求将消息发布到松弛通道\用户
- ios - SwiftUI - 如何在不滚动的情况下将多行文本包装在 UITextView 中?
- python - Tensorflow 2.1.0 - DLL 加载失败 - windows cpu
- linux - docker-compose 卷集主机路径
- javascript - Yandex Metric第一次点击响应长