我有一个代码如下,它基本上使用plotly并绘制数据框的多列。 下面的代码工作正常。但我想用这个图作为子图。想象一下,有 3 行和 2 列,并希望将此函数的类似图形显示为子图。
def custom_graph_objects(df, item, show=False):
# Create the Plotly figure
fig = go.Figure()
# Add Percentage Change trace with markers and labels
fig.add_trace(go.Scatter(x=df['vpl_start_week'][df['wh_item_code']==item], y=df['list_price'][df['wh_item_code']==item], mode='lines', name='list_price'))
fig.add_trace(go.Scatter(x=df['vpl_start_week'][df['wh_item_code']==item], y=df['current_sell_zone1'][df['wh_item_code']==item], mode='lines', name='current_sell_zone1'))
# Add Percentage Change From Initial trace with markers and labels
fig.add_trace(go.Scatter(x=df['vpl_start_week'][df['wh_item_code']==item], y=df['percentile_difference'][df['wh_item_code']==item], mode='lines',
name='percentile_difference'))
# Set the title and axis labels
fig.update_layout(title='Item Price Analysis',
xaxis_title='vpl_start_date',
yaxis_title='price')
fig.update_traces(mode="lines", hovertemplate=None)
fig.update_layout(hovermode="x unified")
if show:
fig.show()
return fig
下面是我尝试的子图代码,但它没有反映我需要的。
r = 2
c = 2
sample_items = random.sample(grand_aggregate['wh_item_code'].unique().tolist(), r*c)
sub_fig = make_subplots(rows=r, cols=c)
pos = [(x+1, y+1) for x in range(r) for y in range(c)]
for i, item in enumerate(sample_items):
sub_fig.add_trace(go.Scatter(x=grand_aggregate['vpl_start_week'][grand_aggregate['wh_item_code']==item],
y=grand_aggregate['list_price'][grand_aggregate['wh_item_code']==item], mode='lines', name='list_price'),
row=pos[i][0], col=pos[i][1])
sub_fig.update_traces(go.Scatter(x=grand_aggregate['vpl_start_week'][grand_aggregate['wh_item_code']==item],
y=grand_aggregate['current_sell_zone1'][grand_aggregate['wh_item_code']==item], mode='lines', name='current_sell_zone1'),
row=pos[i][0], col=pos[i][1])
sub_fig.update_traces(go.Scatter(x=grand_aggregate['vpl_start_week'][grand_aggregate['wh_item_code']==item],
y=grand_aggregate['percentile_difference'][grand_aggregate['wh_item_code']==item], mode='lines', name='percentile_difference'),
row=pos[i][0], col=pos[i][1])
sub_fig.update_layout(height=900, width=1200, title_text="Subplots")
sub_fig.show()
非常感谢对此的任何帮助。预先感谢。
plotly 中的子图使用 make_subplots(rows=r, cols=c) 创建一个对象,并将图形设置为子图,该子图是对象的矩阵之一。因此函数中不需要 'fig=go.Figure()',并且需要Fig作为参数。示例数据有多列,但我创建了一个函数将其扩展为子图,然后创建代码以在循环过程中完成整个过程。
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = px.data.stocks()
df.head()
date GOOG AAPL AMZN FB NFLX MSFT
0 2018-01-01 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
1 2018-01-08 1.018172 1.011943 1.061881 0.959968 1.053526 1.015988
2 2018-01-15 1.032008 1.019771 1.053240 0.970243 1.049860 1.020524
3 2018-01-22 1.066783 0.980057 1.140676 1.016858 1.307681 1.066561
4 2018-01-29 1.008773 0.917143 1.163374 1.018357 1.273537 1.040708
r,c = 3,2
sample_tickers = df.columns.tolist()[1:]
sub_fig = make_subplots(rows=r, cols=c)
pos = [(x+1, y+1) for x in range(r) for y in range(c)]
def custom_graph(df, tick, sub_fig, pos, show=False):
sub_fig.add_trace(go.Scatter(
x=df['date'],
y=df[tick],
mode='lines'
), row=pos[0], col=pos[1])
return sub_fig
for i,(tick,p) in enumerate(zip(sample_tickers,pos)):
sub_fig = custom_graph(df, tick, sub_fig, p, show=False)
sub_fig.update_layout(height=900, width=1200, title_text='Subplots')
sub_fig.show()