【问题标题】:Is there a way to save multiple plots to html in plotly有没有办法将多个绘图保存到 html 中
【发布时间】:2016-07-04 05:12:44
【问题描述】:

我想生成多个独立的图并将它们保存到一个 html 文件中,使用 plotly 离线图。有没有办法做到这一点?

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    我希望以下来自here 的示例能够满足您的需求:

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    import mpld3
    from mpld3 import plugins
    
    # Define some CSS to control our custom labels
    css = """
    table
    {
      border-collapse: collapse;
    }
    th
    {
      color: #ffffff;
      background-color: #000000;
    }
    td
    {
      background-color: #cccccc;
    }
    table, th, td
    {
      font-family:Arial, Helvetica, sans-serif;
      border: 1px solid black;
      text-align: right;
    }
    """
    
    fig, ax = plt.subplots()
    ax.grid(True, alpha=0.3)
    
    N = 50
    df = pd.DataFrame(index=range(N))
    df['x'] = np.random.randn(N)
    df['y'] = np.random.randn(N)
    df['z'] = np.random.randn(N)
    
    labels = []
    for i in range(N):
        label = df.ix[[i], :].T
        label.columns = ['Row {0}'.format(i)]
        # .to_html() is unicode; so make leading 'u' go away with str()
        labels.append(str(label.to_html()))
    
    points = ax.plot(df.x, df.y, 'o', color='b',
                     mec='k', ms=15, mew=1, alpha=.6)
    
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('HTML tooltips', size=20)
    
    tooltip = plugins.PointHTMLTooltip(points[0], labels,
                                       voffset=10, hoffset=10, css=css)
    plugins.connect(fig, tooltip)
    
    mpld3.show()
    

    输出:

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 2021-12-18
      • 2010-11-14
      • 1970-01-01
      相关资源
      最近更新 更多