1

I have a dataframe as shown below with 3 columns. I am using clump as my x values and Unif size as my y values to form a scatterplot. But I want to color the individual points based on the third column class. Points having class values 2 as green and 4 as blue.

So taking the first and last points in the dataframe as examples. The first point will have an x-value of 5, y-value of 1 with color green, while the last point will have an x-value of 4, y-value of 8 and color blue

I tried using if statement as shown, but I get syntax errors. Any ideas on how to do this?

 fig = go.Figure()
 fig.update_layout(width = 400, height = 400, template = 'plotly_white',xaxis_title = 'clump', yaxis_title = 'Unif Size')
 fig.add_trace(go.Scatter(x = data.Clump,
                          y = data.UnifSize,
                          mode = 'markers',
                          if data.Class == 2:
                              marker = duct(
                              color = 'green'
                              ) 
                          if data.Class == 4:
                             marker = dict(
                             color = 'yellow'
                             )
                     )))

enter image description here

zabop
  • 6,750
  • 3
  • 39
  • 84
imantha
  • 2,676
  • 4
  • 23
  • 46

1 Answers1

0

You can do for example this:

Create example x and y data, with an array containing the condition on which the color will depend:

import numpy as np
x = [x for x in range(100)]
y = [3*each*np.random.normal(loc=1.0, scale=0.1) for each in range(100)]
condition = [np.random.randint(0,2) for x in range(100)]

The x and y points which have an index which corresponds to a 0 in the condition array are:

[eachx for indexx, eachx in enumerate(x) if condition[indexx]==0]
[eachy for indexy, eachy in enumerate(y) if condition[indexy]==0]

If we want the elements in the x and y arrays which have an index corresponding to a 1 in the condition array we just change the 0 to 1:

[eachx for indexx, eachx in enumerate(x) if condition[indexx]==1]
[eachy for indexy, eachy in enumerate(y) if condition[indexy]==1]

Alternatively, you could use zip:

[eachx for eachx, eachcondition in zip(x, condition) if eachcondition==0]

And so on for the others.

This is list comprehension with a condition, well explained here: https://stackoverflow.com/a/4260304/8565438.

Then plot the 2 pair of arrays with 2 go.Scatter calls.

The whole thing together:

import numpy as np
x = [x for x in range(100)]
y = [3*each*np.random.normal(loc=1.0, scale=0.1) for each in range(100)]
condition = [np.random.randint(0,2) for x in range(100)]

import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(width = 400, height = 400, template = 'plotly_white',xaxis_title = 'clump', yaxis_title = 'Unif Size')
fig.add_trace(go.Scatter(x = [eachx for indexx, eachx in enumerate(x) if condition[indexx]==0],
                        y = [eachy for indexy, eachy in enumerate(y) if condition[indexy]==0],
                        mode = 'markers',marker = dict(color = 'green')))
fig.add_trace(go.Scatter(x = [eachx for indexx, eachx in enumerate(x) if condition[indexx]==1],
                        y = [eachy for indexy, eachy in enumerate(y) if condition[indexy]==1],
                        mode = 'markers',marker = dict(color = 'yellow')))
fig.show()

This will give you:

enter image description here

Which is what we wanted I believe.


For converting to list from DataFrame column, recommend this: get list from pandas dataframe column.

zabop
  • 6,750
  • 3
  • 39
  • 84