What is wrong with this example?

I try to obtain plot color depending value of class.
Here is the example that looks like what I want

import plotly.graph_objs as go
import plotly.plotly as py
import plotly.tools as tls
import plotly.figure_factory as ff

import copy
import numpy as np
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris-data.csv')


py.iplot(df_table, filename='iris-data-head')
classes=np.unique(df['class'].values).tolist()

class_code={classes[k]: k for k in range(3)}

color_vals=[class_code[cl] for cl in df['class']]

pl_colorscale=[[0.0, '#19d3f3'],
               [0.333, '#19d3f3'],
               [0.333, '#e763fa'],
               [0.666, '#e763fa'],
               [0.666, '#636efa'],
               [1, '#636efa']]

text=[df.loc[ k, 'class'] for k in range(len(df))]

trace1 = go.Splom(dimensions=[dict(label='sepal length',
                                 values=df['sepal length']),
                            dict(label='sepal width',
                                 values=df['sepal width']),
                            dict(label='petal length',
                                 values=df['petal length']),
                            dict(label='petal width',
                                 values=df['petal width'])],
                text=text,
                #default axes name assignment :
                #xaxes= ['x1','x2',  'x3'],
                #yaxes=  ['y1', 'y2', 'y3'], 
                marker=dict(color=color_vals,
                            size=7,
                            colorscale=pl_colorscale,
                            showscale=False,
                            line=dict(width=0.5,
                                      color='rgb(230,230,230)'))
                )

axis = dict(showline=True,
          zeroline=False,
          gridcolor='#fff',
          ticklen=4)

layout = go.Layout(
    title='Iris Data set',
    dragmode='select',
    width=600,
    height=600,
    autosize=False,
    hovermode='closest',
    plot_bgcolor='rgba(240,240,240, 0.95)',
    xaxis1=dict(axis),
    xaxis2=dict(axis),
    xaxis3=dict(axis),
    xaxis4=dict(axis),
    yaxis1=dict(axis),
    yaxis2=dict(axis),
    yaxis3=dict(axis),
    yaxis4=dict(axis)
)

fig1 = dict(data=[trace1], layout=layout)
py.iplot(fig1, filename='splom-iris1')

But it is not working. Gives the error that py is not defined. So I added
import chart_studio.plotly as py
But still does not work

@gosforth If you are running plotly version 4.+, then remove from the above code the lines:

import plotly.plotly as py

py.iplot(df.table, filename='iris-data-head')

Replace

fig1 = dict(data=[trace1], layout=layout)

by

fig1 = go.Figure(data=[trace], layout=layout)

and

py.iplot(fig1, filename='splom-iris1')

by:

fig1.show( )

If you have a Plotly account and want to send your plot to Plotly cloud,
then add the following lines of code:

import chart_studio.plotly as py
py.sign_in('username', 'api_key')
py.iplot(fig1, filename='yourfigname')

Thank you very much indeed. Works.
However I still do not get how to obtain different colors for different class.

In this example there are three classes:

['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']

and three class codes:

{'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2}

So I would expect that pl_colorscale will be a list of three colrs for code 1, 2 and 3… But I see there 6 items:

pl_colorscale=[[0.0, '#19d3f3'],
               [0.333, '#19d3f3'],
               [0.333, '#e763fa'],
               [0.666, '#e763fa'],
               [0.666, '#636efa'],
               [1, '#636efa']]

What is 0.0, 0.333, 0.666… ??
It is not a class code…

If in my csv file I have four classes then what should be there?
Regards

@gosforth The colorscale in the iris example is a discrete colorscale with three colors corresponding to the three flower classes. More precisely, colormapping data in an interval [a,b] consists in two steps:
first the interval [a, b] is mapped to [0,1], i.e. data are normalized. Then each value in [0,1] is mapped via linear interpolation to a Plotly colorscale of the form:

[[0.0, 'rgb(253, 253, 204)'],
 [0.1, 'rgb(201, 235, 177)'],
 [0.2, 'rgb(145, 216, 163)'],
 [0.3, 'rgb(102, 194, 163)'],
 [0.4, 'rgb(81, 168, 162)'],
 [0.5, 'rgb(72, 141, 157)'],
 [0.6, 'rgb(64, 117, 152)'],
 [0.7, 'rgb(61, 90, 146)'],
 [0.8, 'rgb(65, 64, 123)'],
 [0.9, 'rgb(55, 44, 80)'],
 [1.0, 'rgb(39, 26, 44)']]

For example if [a, b] =[-1, 3] then the map f:[a,b]–>[0,1] is defined as follows:

f(x)=(x-a)/(b-a)

Such a map however is defined by the plotly.js, not the user in his code.

If your value is -0.5, then f(-0.5) =(-0.5+1)/(3+1)=0.5/4=1/8 =0.125. This normalized value, 0.125 is mapped by plotly.js to a color computed by linear interpolation. That color β€œlies” between the color in the colorscale associated to 0.1, i.e. β€˜rgb(201, 235, 177)’, and the color associated to 0.2, β€˜rgb(145, 216, 163)’.

When your data is discrete, consisting in four classes, encoded 0, 1, 2, 3, then the colorscale is a discrete one
defined as follows (here I use hexcolor codes):

my_colorscale = [[0, '#700208'], 
                 [0.25, '#700208'], 
                 [0.25, '#ba2461'], 
                 [0.5, '#ba2461'], 
                 [0.5, '#e081c3'], 
                 [0.75, '#e081c3'],
                 [0.75, '#e6bae4'],
                 [1, '#e6bae4']]

How is interpreted this discrete colorcale? Any normalized value between [0, 0.25] is mapped to the color
'#700208' (i.e. in this case the linear interpolation of the colors associated to the interval ends is the same and equal to ''#700208'). Any value between (0.25, 0.5] is mapped to the color ' '#ba2461', and so on.

Now the normalized codes for the classes 0, 1, 2, 3, are 0, 1/3, 2/3, 3/3=1.

0 is mapped to the color ''#700208'
1/3 to ''#ba2461''
2/3 to '#e081c3'
1 to ''#e6bae4'

Thank you very much for this explanation. I still do not get why to complex such simple thing and introduce complex mapping for colors :slight_smile: There should just be class=>color. Even if that’s needed (for some reason) on software level, coders should make it simple for final user. Possible to define custom colors in plotly express?

I explained how discrete colormapping works, to be able to define yourself a discrete colorscake for your your needs. The same steps follows matplotlib, matlab, etc i.e they are approaching in different way discrete colormapping vs continuous colormapping.

Once you defined the colorscale its use is straightforward.