首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Panda,Plot,Scatter

Panda,Plot,Scatter
EN

Stack Overflow用户
提问于 2021-10-05 15:02:15
回答 2查看 54关注 0票数 0

以下是我的数据集中的一个示例

代码语言:javascript
复制
-0.76,-0.66,-1
0.07,0.59,1
0.73,0.6,-1
0.58,-0.46,-1
-0.71,0.9,-1
-0.82,-0.13,-1
0.2,0.43,1
-0.72,-0.93,-1
0.8,-0.79,-1
-0.35,-0.52,-1
0.53,0.72,1
-0.7,0.96,-1

每列3列

我需要通过在2D图上为每对特征值(即数据中的每一行)放置一个标记来可视化这一点。在图中,x轴应该是第一个要素的值,y轴应该是第二个要素的值,并且标记应该是,例如,当目标值为+1时为+标记,当目标为−1时为o

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-05 17:46:34

根据您所说的,我已经为您提供了一些想法来绘制您的数据帧的所需输出。所以我试着用这段代码重现你的问题:

代码语言:javascript
复制
import pandas as pd

# I made this solution using matplotlib to make the scatterplot
from matplotlib import pyplot as plt

data = [
    (-0.76, -0.66, -1),
    ( 0.07,  0.59,  1),
    ( 0.73,  0.60, -1),
    ( 0.58, -0.46, -1),
    (-0.71,  0.90, -1),
    (-0.82, -0.13, -1),
    ( 0.20,  0.43,  1),
    (-0.72, -0.93, -1),
    ( 0.80, -0.79, -1),
    (-0.35, -0.52, -1),
    ( 0.53,  0.72,  1),
    (-0.70,  0.96, -1),
]

df = pd.DataFrame(data, columns = ["X", "Y", "Sign"])

x = df['X']  # Values for x-axis
y = df['Y']  # Values for y-axis
signs = df['Sign']  # Values for changing the marker in the plots

for i in range(len(x)):
    plt.scatter(
        x[i], y[i],     # X, Y coordinates
        s = 100,        # Size of the markers
        linewidth = 3,  # Line width
        marker = "+" if signs[i] > 0 else "_",      # Control wether the marker is a '+' or a '-'
        color = "green" if signs[i] > 0 else "red"  # Control the color based on minus or plus sign
    )

产生的输出为:

票数 0
EN

Stack Overflow用户

发布于 2021-10-05 17:34:13

如果这是第三列中仅有的两个选项,那么您可以很容易地拆分z=1和z=-1的数据集,然后使用两个绘图函数在同一张图上用不同的标记显示它们。

代码语言:javascript
复制
df1 = df[ df['z'] == 1 ].copy(deep=true)
df2 = df[ df['z'] == -1].copy(deep=true)
plt.plot(df1['x'],df1['y'], marker='+')
plt.plot(df2['x'],df2['y'], marker='o')
plt.show()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69452891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档