Convert plotly.express in to plotly.graph_objects

Hi Guys,

I trying to replicate a cahr created in plotly.experss to plotly.graph_objects but a failing in every times.
I have this dataframe base.

Machine	Time	Units Pruduced
0	K2K01	2	579
1	K2K01	3	350
2	K2K01	4	848
3	K2K01	5	840
4	K2K01	15	768
5	K2K01	17	1752
6	K2K02	1	712
7	K2K02	2	804
8	K2K02	3	327...

And i build char in plotly.express.

fig = px.scatter(fdr4, x="Time", y="Units Pruduced", color="Machine", template="plotly_white")
fig.update_traces(mode='lines+markers')
fig.show()

This is the result.

Also i tryed build in plotly.graph_objects.
PS: This is the more closer i get from expected result.

figure = {
            'data' : [
                go.Scatter(
                
                x = fdr4.index,
                y = fdr4.UnitsProd,
                mode = "markers+lines",
                name = rowname
                )for rowname in fdr4.Machine

And this is the result.

So, could please someone advise me or give some reference what is incorrect in my logic with plotly.graph_objects that graph to generate the same chart as plotly.experss with the same datas?

Thank you.

With the color parameter, Plotly Express will group your data such that each “Machine” in your data has a separate “trace”, along with its own corresponding color in the default colorway.

Plotly Express simply does this grouping internally to generate a figure with the same syntax as plotly.graph_objects. In order to see the figure it generates, you can print(fig), or

import pdb; pdb.set_trace()

and inspect the fig in the Pdb REPL. Then you can copy-and-paste the generated 'data' list and 'figure' dict into your figure dict that is supplied as an argument for plotly.graph_objects. The result should then be the same.

Also, FYI, a more appropriate category for this question is in the API > Python category. In the future, you may get more attention on Plotly Express/Graph Objects-related Python questions if you use that category instead of “Dash”. I changed the category myself just now.