如何在单个浏览器窗口中获取for循环显示内创建的所有Plotly图?

问题描述 投票:0回答:1

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

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

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)

我希望所有图表都显示在带滚动条的单个浏览器窗口中。

python plotly
1个回答
1
投票

您可以移动在循环外部声明图形的点,并为其提供更多行或列。例如,使用与数据点一样多的列来生成数字。然后将图表放在第i列中。就像是:

# 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)
© www.soinside.com 2019 - 2024. All rights reserved.