myth_zy 2023-09-13 17:16 采纳率: 0%
浏览 15
已结题

如何利用python plotly实现多html合并后共享或联动按钮

#我通过以下代码实现了plotly生成的多个离线html图表合并展示在一个html文件中,每个图表都有按钮,目前的代码只能实现一个按钮控制一张图,如何共享按钮或者联动按钮,实现一次点击所有图表都能响应呢?希望有朋友能帮助解答

import numpy as np
import plotly
import plotly.graph_objects as go

fichier_html_graphs=open("DASHBOARD.html",'w')
fichier_html_graphs.write("<html><head></head><body>"+"\n")

i=0
while 1:
    if i<=5:
        i=i+1


        #______________________________--Plotly--______________________________________
        # 定义三组数据
        x0 = np.random.normal(2, 0.4, 400)
        y0 = np.random.normal(2, 0.4, 400)
        x1 = np.random.normal(3, 0.6, 600)
        y1 = np.random.normal(6, 0.4, 400)
        x2 = np.random.normal(4, 0.2, 200)
        y2 = np.random.normal(4, 0.4, 200)

        # 先绘制散点图
        trace0 = go.Scatter(
            x=x0,
            y=y0,
            mode='markers',
            marker=dict(color='#835AF1')
        )
        trace1 = go.Scatter(
            x=x1,
            y=y1,
            mode='markers',
            marker=dict(color='#7FA6EE')
        )
        trace2 = go.Scatter(
            x=x2,
            y=y2,
            mode='markers',
            marker=dict(color='#B8F7D4')
        )
        data = [trace0, trace1, trace2]

        # 绘制三个圆型图
        cluster0 = [dict(type='circle',
                         xref='x', yref='y',
                         x0=min(x0), y0=min(y0),
                         x1=max(x0), y1=max(y0),
                         opacity=.25,
                         line=dict(color='#835AF1'),
                         fillcolor='#835AF1')]
        cluster1 = [dict(type='circle',
                         xref='x', yref='y',
                         x0=min(x1), y0=min(y1),
                         x1=max(x1), y1=max(y1),
                         opacity=.25,
                         line=dict(color='#7FA6EE'),
                         fillcolor='#7FA6EE')]
        cluster2 = [dict(type='circle',
                         xref='x', yref='y',
                         x0=min(x2), y0=min(y2),
                         x1=max(x2), y1=max(y2),
                         opacity=.25,
                         line=dict(color='#B8F7D4'),
                         fillcolor='#B8F7D4')]

        #设置五个按钮
        updatemenus = list([
            dict(type="buttons",
                 buttons=list([
                     dict(label='None',
                          method='relayout',
                          args=['shapes', []]),
                     dict(label='Cluster 0',
                          method='relayout',
                          args=['shapes', cluster0]),
                     dict(label='Cluster 1',
                          method='relayout',
                          args=['shapes', cluster1]),
                     dict(label='Cluster 2',
                          method='relayout',
                          args=['shapes', cluster2]),
                     dict(label='All',
                          method='relayout',
                          args=['shapes', cluster0 + cluster1 + cluster2])
                 ]),
                 )
        ])

        layout = dict(title='Highlight Clusters', showlegend=False,
                      updatemenus=updatemenus)

        fig = dict(data=data, layout=layout)
        fig = go.Figure(data=data, layout=layout)
        plotly.offline.plot(fig, filename='Chart_'+str(i)+'.html',auto_open=False)
        fichier_html_graphs.write("  <object data=\""+'Chart_'+str(i)+'.html'+"\" width=\"650\" height=\"500\"></object>"+"\n")
    else:
        break

fichier_html_graphs.write("</body></html>")

img

  • 写回答

8条回答 默认 最新

  • 玥轩_521 新星创作者: 网络协议与安全技术领域 2023-09-13 17:24
    关注

    援引通义千问:
    要实现共享或联动按钮,可以将每个图表的按钮都添加到一个共享的updatemenus中,然后在每个图表的layout中引用这个共享的updatemenus。这样,每个图表的按钮都可以控制所有图表的显示。
    以下是修改后的代码示例:

    import numpy as np
    import plotly
    import plotly.graph_objects as go
    
    fichier_html_graphs=open("DASHBOARD.html",'w')
    fichier_html_graphs.write("<html><head></head><body>"+"\n")
    
    updatemenus = list([
        dict(type="buttons",
             buttons=list([
                 dict(label='None',
                      method='relayout',
                      args=['shapes', []]),
                 dict(label='Cluster 0',
                      method='relayout',
                      args=['shapes', cluster0]),
                 dict(label='Cluster 1',
                      method='relayout',
                      args=['shapes', cluster1]),
                 dict(label='Cluster 2',
                      method='relayout',
                      args=['shapes', cluster2]),
                 dict(label='All',
                      method='relayout',
                      args=['shapes', cluster0 + cluster1 + cluster2])
             ]),
             )
    ])
    
    i=0
    while 1:
        if i<=5:
            i=i+1
            #______________________________--Plotly--______________________________________
            # 定义三组数据
            x0 = np.random.normal(2, 0.4, 400)
            y0 = np.random.normal(2, 0.4, 400)
            x1 = np.random.normal(3, 0.6, 600)
            y1 = np.random.normal(6, 0.4, 400)
            x2 = np.random.normal(4, 0.2, 200)
            y2 = np.random.normal(4, 0.4, 200)
            # 先绘制散点图
            trace0 = go.Scatter(
                x=x0,
                y=y0,
                mode='markers',
                marker=dict(color='#835AF1')
            )
            trace1 = go.Scatter(
                x=x1,
                y=y1,
                mode='markers',
                marker=dict(color='#7FA6EE')
            )
            trace2 = go.Scatter(
                x=x2,
                y=y2,
                mode='markers',
                marker=dict(color='#B8F7D4')
            )
            data = [trace0, trace1, trace2]
            # 绘制三个圆型图
            cluster0 = [dict(type='circle',
                             xref='x', yref='y',
                             x0=min(x0), y0=min(y0),
                             x1=max(x0), y1=max(y0),
                             opacity=.25,
                             line=dict(color='#835AF1'),
                             fillcolor='#835AF1')]
            cluster1 = [dict(type='circle',
                             xref='x', yref='y',
                             x0=min(x1), y0=min(y1),
                             x1=max(x1), y1=max(y1),
                             opacity=.25,
                             line=dict(color='#7FA6EE'),
                             fillcolor='#7FA6EE')]
            cluster2 = [dict(type='circle',
                             xref='x', yref='y',
                             x0=min(x2), y0=min(y2),
                             x1=max(x2), y1=max(y2),
                             opacity=.25,
                             line=dict(color='#B8F7D4'),
                             fillcolor='#B8F7D4')]
            #设置五个按钮
            layout = dict(title='Highlight Clusters', showlegend=False,
                          updatemenus=updatemenus)
     
            fig = dict(data=data, layout=layout)
            fig = go.Figure(data=data, layout=layout)
            plotly.offline.plot(fig, filename='Chart_'+str(i)+'.html',auto_open=False)
            fichier_html_graphs.write("  <object data=\""+'Chart_'+str(i)+'.html'+"\" width=\"650\" height=\"500\"></object>"+"\n")
        else:
            break
    
    fichier_html_graphs.write("</body></html>")
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月14日
  • 创建了问题 9月13日