简体   繁体   中英

Adding dynamic horizontal line to Multiple Facets / Subplots in plotly

Similar to the example in the documentation, but I don't want to rebase the performance leaving just the price of each stock

Then I want the starting price drawn horizontally across chart. The starting price of google will be different to facebook, so the line has to know that it is the facebook price on the facebook chart and so on. Basically replacing the 'y=1' with a dynamic peice of code. Possible?

https://plotly.com/python/horizontal-vertical-shapes/

import plotly.express as px

df = px.data.stocks(indexed=True)
fig = px.line(df, facet_col="company", facet_col_wrap=2)
fig.add_hline(y=1, line_dash="dot", row="all", col="all")

fig.show()

The sample data is the coefficient of increase or decrease with the base date set to 0, so all stocks are set to 0. The average of each stock is obtained and the horizontal line is added with the average value. If your data is stock prices, you can use the same method to set the first value.

import plotly.express as px

df = px.data.stocks(indexed=True)
fig = px.line(df, facet_col="company", facet_col_wrap=2)

for ticker,idx in zip(df.columns,[(3,1),(3,2),(2,1),(2,2),(1,1),(1,2)]):
    means = df[ticker].mean()
    print(ticker, means)
    fig.add_hline(y=means, line_dash="dot", row=idx[0], col=idx[1])

fig.show()

GOOG 1.0462059136048643
AAPL 1.1385360556190478
AMZN 1.393822068838919
FB 0.9451212162327117
NFLX 1.5405590371517246
MSFT 1.3180589260425406

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM