How can I get all Plotly plots created inside a for loop display in single browser window?

被刻印的时光 ゝ 提交于 2021-01-29 05:46:06

问题


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.

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.


回答1:


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)


来源:https://stackoverflow.com/questions/55845360/how-can-i-get-all-plotly-plots-created-inside-a-for-loop-display-in-single-brows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!