Home > Mobile >  Panda, Plot, Scatter
Panda, Plot, Scatter

Time:10-06

Here's a sample from my dataset

-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 columns in each

I need to visualize this as such that by placing a marker on a 2D plot for each pair of feature values i.e. for each row in the data. On the plot the x-axis should be the value of the first feature, the y-axis the value of the second feature and the marker should be, for example, a marker when the target value is 1 and a o when the target is −1

CodePudding user response:

If those are your only two options for the third column you could pretty easily split the dataset where z=1 and z=-1, then use two plot functions to show them both on the same graph with different markers.

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

CodePudding user response:

Based on what you told, I've had some ideas for you to plot the desired output from your dataframe. So I tried to reproduce your problem with this code:

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
    )

The output produced is:

Output produced

  • Related