create subplots from dict using plotly in python

Here is an example of how to create subplots from a dictionary using plotly in Python:

main.py
import plotly.graph_objs as go

# Create dictionary of data
data_dict = {
    'trace1': [1, 2, 3],
    'trace2': [4, 5, 6],
    'trace3': [7, 8, 9]
}

# Create subplot layout
fig = plotly.subplots.make_subplots(rows=1, cols=3)

# Add traces to subplot
trace1 = go.Scatter(y=data_dict['trace1'], mode='lines', name='Trace 1')
trace2 = go.Bar(y=data_dict['trace2'], name='Trace 2')
trace3 = go.Scatter(y=data_dict['trace3'], mode='markers', name='Trace 3')
fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
fig.add_trace(trace3, row=1, col=3)

# Update subplot layout
fig.update_layout(title='Subplot Example',
                  xaxis=dict(title='X Axis'),
                  yaxis=dict(title='Y Axis'))

# Show figure
fig.show()
749 chars
28 lines

In this example, we first create a dictionary (data_dict) of the data we want to plot as traces. We then create a subplot layout using make_subplots(), setting the rows and cols parameters to the desired number of subplots. We then create trace objects for each of the data dictionary keys and add them to the subplot using add_trace(), specifying the row and column numbers to place them in.

Finally, we update the layout with a title and axis labels using update_layout(), and then show the figure using show().

related categories

gistlibby LogSnag