如何在单个浏览器窗口中显示在 for 循环中创建的所有 Plotly 图? [英] How can I get all Plotly plots created inside a for loop display in single browser window?

查看:49
本文介绍了如何在单个浏览器窗口中显示在 for 循环中创建的所有 Plotly 图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 plotly 在 for 循环中创建多个图.目前所有图表都出现在浏览器的单独选项卡中.我希望所有图表都显示在同一个浏览器窗口中.

I am trying to create several plots inside a for loop using plotly. Currently all the charts appear in separate tabs of browser. I want all the charts to appear in the same browser window.

在我的数据框 df 中,对于 Tool_MeasurementSet 列中的每个唯一元素(保存为列表 meas_set 的唯一元素)具有 16 个 X-BAR 数据点和 16 个 SIGMA 数据点.我能够使用 subplot 函数为 meas_set 中的每个元素组合 X-BAR 和 SIGMA 图.目前,代码正在浏览器的单独选项卡中为 meas_set 列表中的每个元素创建绘图.但我想让所有的图都显示在带有垂直滚动条的同一个浏览器窗口中,而不必从一个选项卡移动到另一个选项卡来查看图.

In my data frame df, for each unique element in Tool_MeasurementSet column (unique elements saved as a list meas_set) has 16 data points for X-BAR and 16 for SIGMA. I was able to use subplot function to combine X-BAR and SIGMA plot for each element in meas_set. Currently the code is creating plots for each element in meas_set list in a separate tab of the browser. But I want to make all the plots appear in the same browser window with a vertical scroll bar instead of having to move from one tab to another to look at plots.

from plotly import tools
import plotly.plotly as py
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.offline as pyo
import plotly.graph_objs as go

df = pd.read_csv("C:\\DATA_REPORT_subset.csv")
meas_set = df['Tool_MeasurementSet'].unique()

## params are the column labels in the df dataframe
params = ['Data','UCL','LCL','CL']

for i in meas_set:
    fig = tools.make_subplots(rows=2, cols=1,subplot_titles=('X-BAR Subplot','SIGMA Subplot'))
    for j in range(0,len(params)):
        y_xbar = df[(df['Tool_MeasurementSet']== i) & (df['Chart Type']== 'X-BAR')][params[j]]
        x_xbar = df[(df['Tool_MeasurementSet']== i) & (df['Chart Type']== 'X-BAR')]['Date']
        y_sigma = df[(df['Tool_MeasurementSet']== i) & (df['Chart Type']== 'SIGMA')][params[j]]
        x_sigma = df[(df['Tool_MeasurementSet']== i) & (df['Chart Type']== 'SIGMA')]['Date']
        trace1 = go.Scatter(x=x_xbar,y=y_xbar,mode='lines',name=params[j])
        trace2 = go.Scatter(x=x_sigma,y=y_sigma,mode='lines',name=params[j])

        fig.append_trace(trace1,1,1)
        fig.append_trace(trace2,2,1)

    fig['layout'].update(title= i)
    pyo.plot(fig)

我希望所有绘图都显示在一个带有滚动条的浏览器窗口中.

I want all the plots to appear in a single browser window with a scroll bar.

推荐答案

您可以将声明图形的点移到循环之外,并为其添加更多行或列.例如,制作一个包含与数据点一样多的列的图形.然后将图放在第 i 列中.类似的东西:

You could just move the point where you declare the figure outside of the loop and give it more rows or columns. For example, make a figure with as many columns as there are datapoints. Then put the plots in the ith column. Something like:

# use len(meas_set) as number of columns
fig = tools.make_subplots(rows=2, cols=len(meas_set), subplot_titles=('X-BAR Subplot','SIGMA Subplot'))

for i in meas_set:
    for j in range(0,len(params)):
        # your logic here
        trace1 = go.Scatter(x=x_xbar,y=y_xbar,mode='lines',name=params[j])
        trace2 = go.Scatter(x=x_sigma,y=y_sigma,mode='lines',name=params[j])

        # use i for column position
        fig.append_trace(trace1,1,i)
        fig.append_trace(trace2,2,i)

这篇关于如何在单个浏览器窗口中显示在 for 循环中创建的所有 Plotly 图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆