Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign colors for scatterplot by group?

Tags:

python

plotly

I'm trying to assign color for each class in my dataframe in plotly, here is my code:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

knn = KNeighborsClassifier(n_neighbors=7)

# fitting the model
knn.fit(X_train, y_train)

# predict the response
pred = knn.predict(X_test)

dfp = pd.DataFrame(X_test)
dfp.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']
dfp["PClass"] = pred

pyo.init_notebook_mode()
data = [go.Scatter(x=dfp['SepalLengthCm'], y=dfp['SepalWidthCm'], 
                   text=dfp['PClass'],
                   mode='markers',
                   marker=dict(
                    color=dfp['PClass']))]

layout = go.Layout(title='Chart', hovermode='closest')
fig = go.Figure(data=data, layout=layout)

pyo.iplot(data)

And here how my df looks like:

SepalLengthCm   SepalWidthCm    PetalLengthCm   PetalWidthCm    PClass
       6.1           2.8             4.7         1.2    Iris-versicolor
      5.7            3.8             1.7         0.3        Iris-setosa
      7.7             2.6        6.9         2.3    Iris-virginica

So the problem is that it's not assigning color based on dfp['PClass'] column and every point on the plot is the same color: black. Even though when hovering every point is correctly labeled based on its class. Any ideas why it's not working correctly?

like image 239
Alex T Avatar asked Jul 15 '26 04:07

Alex T


1 Answers

Here is an example using graph objects:

import numpy as np
import pandas as pd
import plotly.offline as pyo
import plotly.graph_objs as go

# Create some random data
np.random.seed(42)
random_x = np.random.randint(1, 101, 100)
random_y = np.random.randint(1, 101, 100)

# Create two groups for the data
group = []
for letter in range(0,50):
    group.append("A")

for letter in range(0, 50):
    group.append("B")

# Create a dictionary with the three fields to include in the dataframe
group = np.array(group)
data = {
    '1': random_x,
    '2': random_y,
    '3': group
}

# Creat the dataframe
df = pd.DataFrame(data)

# Find the different groups
groups = df['3'].unique()

# Create as many traces as different groups there are and save them in data list
data = []
for group in groups:
    df_group = df[df['3'] == group]
    trace = go.Scatter(x=df_group['1'], 
                        y=df_group['2'],
                        mode='markers',
                        name=group)
    data.append(trace)

# Layout of the plot
layout = go.Layout(title='Grouping')
fig = go.Figure(data=data, layout=layout)

pyo.plot(fig)

enter image description here

like image 129
tmontserrat Avatar answered Jul 17 '26 15:07

tmontserrat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!