Different continious color bars for different classes in Python

Viewed 187

I have data points that belong to three different classes. On the other hand, I have weight for each data point in its class. I want to color my points based on their weight, but with three different continuous range of colors. Actually I want something like the following image (which is made by hand). Now I'm using Plotly for coloring, but any other method compatible with python is welcomed.

enter image description here

Actually I want to combine the two output of the code:

if __name__ == '__main__':
n_data = 100
n_class = 3
t1 = [random.random() for i in range(n_data)]
t2 = [random.random() for i in range(n_data)]
class_color = [str(random.randint(1,n_class)) for i in range(n_data)]
weight_color = [random.random() for i in range(n_data)]
df = pd.DataFrame()
print(len(t1))
print(len(t2))
df['x'] = t1
df['y'] = t2
df['class_color'] = class_color
df['weight_color'] = weight_color
fig1 = px.scatter(df, x="x", y="y", color="class_color")
fig1.show()

fig2 = px.scatter(df, x="x", y="y", color="weight_color")
fig2.show()
1 Answers

Please don't take it as an answer (yet). As far as I can see you can use different color scales with plotly. But you should work on how properly show all legends

import plotly.graph_objects as go
import plotly.express as px
df = px.data.iris()
dfs = [d[1] for d in list(df.groupby('species'))]

fig = go.Figure()
fig.add_trace(
    go.Scatter(x=dfs[0]["sepal_width"],
               y=dfs[0]["sepal_length"],mode="markers",
               marker=dict(color=dfs[0]["sepal_length"],
                           colorscale='Viridis',
                           showscale=True),
               name=dfs[0]["species"].unique()[0],
               showlegend=False
               ))

fig.add_trace(
    go.Scatter(x=dfs[1]["sepal_width"],
               y=dfs[1]["sepal_length"],mode="markers",
               marker=dict(color=dfs[1]["sepal_length"],
                           colorscale='Magenta',
                           showscale=False),
               name=dfs[1]["species"].unique()[0],
               showlegend=False
               ))


fig.add_trace(
    go.Scatter(x=dfs[2]["sepal_width"],
               y=dfs[2]["sepal_length"],mode="markers",
               marker=dict(color=dfs[2]["sepal_length"],
                           colorscale='Cividis',
                           showscale=False),
               name=dfs[2]["species"].unique()[0],
               showlegend=False
               ))

enter image description here

Related