how to know plot type from fig.data()?

Question:

I am using subplots, the first subplot is heatmap, and the second subplot is line plot. after plotting, how do I know its plot type ( heatmap or line plot)? I am using fig.data to refer to its data, is it possible to get the plot type from fig.data()? Thanks for your help.

here is my code:


    fig = make_subplots(rows=2, cols=1, vertical_spacing=0.05,
                    specs=[[{"secondary_y": False}],[{"secondary_y": True}],[{"secondary_y":False}]],
                    subplot_titles=('DTS Long Term Heatmap for '+wellname,
                                   'Well Production of '+wellname,
                                   'Steam Injection of its Nearby Injectors'),
                    shared_xaxes=True,
                    row_heights=[0.6, 0.2,0.2],  # new from top to bottom
                   )
                   
                   
    trace0 = go.Heatmap()
    data = [trace0]
    fig.add_trace(trace0,row=1, col=1)
    
    fig.add_trace(go.Scatter(
    x=df.index,
    y=df.values, 
    mode="lines",
    line={"color": 'black'},  #"color": "#035593"
    name='zero line',
    legendgroup = '2', 
    showlegend=False,
        ),
    row=row,
    col=1,
    secondary_y=False
    )

for i, d in enumerate(fig.data):

    if d.name==key_1:
        legendgroup='2'
        row=2
        secondary_y=False
    elif d.name==key_2:
        legendgroup='2'
        row=2
        secondary_y=True
    fig.add_scatter(x=[d.x[-1]], y = [d.y[-1]],
                    mode = 'markers+text',
                    text = f'{d.y[-1]:.2f}',
                    textfont = dict(color=d.line.color),
                    textposition='middle right',
                    marker = dict(color = d.line.color, size = 12),
                    legendgroup = legendgroup, #d.name,
                    secondary_y=secondary_y,
                    row=row,col=1,
                    showlegend=False)
Asked By: roudan

||

Answers:

Yes, you are pretty close. You can check the type of each trace in fig.data by accessing the .type attribute, which would look like this if you use a loop:

for trace in fig.data:
    print(trace.type)

For example:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(
    rows=2, cols=2,
    specs=[[{"type": "xy"}, {"type": "polar"}],
           [{"type": "domain"}, {"type": "scene"}]],
)

fig.add_trace(go.Bar(y=[2, 3, 1]),
              row=1, col=1)

fig.add_trace(go.Barpolar(theta=[0, 45, 90], r=[2, 3, 1]),
              row=1, col=2)

fig.add_trace(go.Pie(values=[2, 3, 1]),
              row=2, col=1)

fig.add_trace(go.Scatter3d(x=[2, 3, 1], y=[0, 0, 0],
                           z=[0.5, 1, 2], mode="lines"),
              row=2, col=2)

Then the following shows the name of each trace type

>>> for trace in fig.data:
        print(trace.type)

bar
barpolar
pie
scatter3d
Answered By: Derek O
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.