6

I want use plot.ly to plot iris dataset. It has an color parameter in scatter data work like hue in seabron but it only change the color of marker and cant change the color of text and i cant find any why to change the marker type for each group of color. the code is :

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",  text='sepal_width')
fig.update_traces(textposition='top center', textfont_size=14, textfont_color=c)
fig.show()

I want the text color be like the color of each point and the 3 difrent type of marker, bc of 2 type of species.

the result is like this and the legend has litel Aa that i don't want be. enter image description here

nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101
Tavakoli
  • 1,303
  • 3
  • 18
  • 36

1 Answers1

12

Update: the Aa has been removed in v5 of Plotly.py, so that portion of the answer below applies to v4 and below.

Here is how you can match the color of text to the marker color, by using for_each_trace:

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",  text='sepal_width')
fig.for_each_trace(lambda t: t.update(textfont_color=t.marker.color, textposition='top center'))
fig.show()

enter image description here

Regarding the legend, it already looks better IMO with my code above, but there is no option to turn this off. You could try an approach where you have two traces, one with just markers and one with just text and have the one with just text not appear in the legend, like so:

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", text='sepal_width')
def add_trace_copy(trace):
    fig.add_traces(trace)
    new_trace = fig.data[-1]
    new_trace.update(textfont_color=trace.marker.color, textposition='top center', 
                     mode="text", showlegend=False)
    trace.update(mode="markers")
fig.for_each_trace(add_trace_copy)
fig.show()

enter image description here

This works, but probably only using the output of Plotly Express functions, which set legendgroup automatically, and might be a bit brittle with more complex figures :)

nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101
  • Excellent answer. Note: the text in the legend was not present for me in the first code block, so Plotly may have removed this behavior. – scottlittle May 04 '22 at 14:38
  • 1
    Yes, in Plotly.py v5 (and the underlying Plotly.js v2) we removed this little `Aa` because of concerns such as this one :) – nicolaskruchten May 05 '22 at 16:01