【问题标题】:how to know plot type from fig.data()?如何从 fig.data() 中知道绘图类型?
【发布时间】:2023-02-18 12:57:48
【问题描述】:

我正在使用子图,第一个子图是热图,第二个子图是线图。绘图后,我如何知道它的绘图类型(热图或线图)?我正在使用 fig.data 来引用它的数据,是否可以从 fig.data() 获取绘图类型?感谢您的帮助。

这是我的代码:


    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)

【问题讨论】:

    标签: python plotly plotly-python


    【解决方案1】:

    是的,你很接近。您可以通过访问 .type 属性来检查 fig.data 中每个跟踪的类型,如果您使用循环,它看起来像这样:

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

    例如:

    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)
    

    然后下面显示了每个跟踪类型的名称

    >>> for trace in fig.data:
            print(trace.type)
    
    bar
    barpolar
    pie
    scatter3d
    

    【讨论】:

      猜你喜欢
      • 2014-05-10
      • 1970-01-01
      • 2010-12-11
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 2020-03-14
      • 1970-01-01
      相关资源
      最近更新 更多