jharm
September 24, 2024, 8:01am
1
Hi I am inititialising my figure and then want to add traces.
But when create a function to do it it does not work. Is this a python thing when passing an object and return the same object?
def update_fig_transient(fig1, fig2, fig3, df, dp):
fig1.add_trace(go.Scatter(x=df['time [s]'],
y=df['OUT'],
mode='lines',
name='Voltage',
legendgroup='Design point {}'.format(dp),
legendgrouptitle_text='Design point {}'.format(dp),
line=dict(color=colors[dp-1])
))
fig2.add_trace(go.Scatter(x=df['time [s]'],
y=df['Load current'],
mode='lines',
name='Current',
legendgroup='Design point {}'.format(dp),
legendgrouptitle_text='Design point {}'.format(dp),
line=dict(color=colors[dp-1])
))
fig3.add_trace(go.Scatter(x=df['time [s]'],
y=df['Inductor current'],
mode='lines',
name='Current',
legendgroup='Design point {}'.format(dp),
legendgrouptitle_text='Design point {}'.format(dp),
line=dict(color=colors[dp-1])
))
return fig1, fig2, fig3
fig1 = go.Figure()
fig2 = go.Figure()
fig3 = go.Figure()
fig1, fig2, fig3 = update_fig_transient(fig1, fig2, fig3, df, dp)
Skiks
September 24, 2024, 2:04pm
2
Hi @jharm ,
I made a quick test, it seems to work:
Code
import numpy as np
from dash import Dash, dcc
import plotly.graph_objects as go
app = Dash(__name__)
x = np.arange(-10, 10)
def update_figs(fig1, fig2, fig3):
fig1.add_scatter(x=x, y=x)
fig2.add_scatter(x=x, y=x ** 2)
fig3.add_scatter(x=x, y=x ** 3)
return fig1, fig2, fig3
fig1 = go.Figure()
fig2 = go.Figure()
fig3 = go.Figure()
fig1, fig2, fig3 = update_figs(fig1, fig2, fig3)
fig1.update_layout(margin={'l': 0, 'r': 0, 't': 0, 'b': 0})
fig2.update_layout(margin={'l': 0, 'r': 0, 't': 0, 'b': 0})
fig3.update_layout(margin={'l': 0, 'r': 0, 't': 0, 'b': 0})
app.layout = [
dcc.Graph(figure=fig1, style={'height': 200}),
dcc.Graph(figure=fig2, style={'height': 200}),
dcc.Graph(figure=fig3, style={'height': 200})
]
if __name__ == "__main__":
app.run(debug=True)
But I think, you don’t need to reassign fig1, fig2, fig3 with the outputs of the function.
That should also work:
Code
import numpy as np
from dash import Dash, dcc
import plotly.graph_objects as go
app = Dash(__name__)
x = np.arange(-10, 10)
def update_figs(fig1, fig2, fig3):
fig1.add_scatter(x=x, y=x)
fig2.add_scatter(x=x, y=x ** 2)
fig3.add_scatter(x=x, y=x ** 3)
fig1 = go.Figure()
fig2 = go.Figure()
fig3 = go.Figure()
update_figs(fig1, fig2, fig3)
fig1.update_layout(margin={'l': 0, 'r': 0, 't': 0, 'b': 0})
fig2.update_layout(margin={'l': 0, 'r': 0, 't': 0, 'b': 0})
fig3.update_layout(margin={'l': 0, 'r': 0, 't': 0, 'b': 0})
app.layout = [
dcc.Graph(figure=fig1, style={'height': 200}),
dcc.Graph(figure=fig2, style={'height': 200}),
dcc.Graph(figure=fig3, style={'height': 200})
]
if __name__ == "__main__":
app.run(debug=True)
Did you try to plot your figures without a function to see if the parameters are fine?
jharm
September 24, 2024, 2:55pm
3
That’s strange. The way I do it. Then I first initialize the figure. And then dependent on input there are 1 or more traces to be added. When I do this using a function it did not work. But if I use a for loop then it worked. Each traces will then be called design point 1 design point 2 etc.
jharm
September 24, 2024, 3:01pm
4
by the way, now when thinking about it. Then I should probably create the complete data frame first in long format and then create the figure in the end.