我正在尝试使用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)
我希望所有图表都显示在带有滚动条的单个浏览器窗口中。
答案 0 :(得分:1)
您可以将声明图形的点移到循环外部,并为其赋予更多的行或列。 例如,使一个图形具有与数据点一样多的列。然后将图放在第ith列中。像这样:
# 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)