如何使用 Plotly Express 绘制一个有两个 Y 轴的图表,一个在左,一个在右?

发布于 2025-01-18 18:53:12 字数 457 浏览 0 评论 0原文

我发现Plotly Express比Plotly更有效。go,这是我的问题: 如何使用Plotly Express绘制一个带有两个Yaxis的图表,一个在Rihgt上绘制一个图表?

我正在尝试绘制一个tickdata图表,该图表用AskPrice和sha sma =“询问价格 - 出价价格”, 原因差和询问价格的规模不同,所以我想绘制两张Yasix图片, 我做到了 但是,两个Yaxis标签都在左侧 我该如何分开Yaxis?

fig = px.line(df, x="datetime", y=\['sma_spread', "sma_plus", "sma_minus"\])  
fig2 = px.scatter(df,x=df\['datetime'\], y=\['a1', "b1"\])  
fig2.update_traces(yaxis ="y2")

show(fig.add_traces(fig2.data))

I found out that Plotly express is more efficient than plotly.go, here is my question:
how to use Plotly express draw a chart with two Yaxis, one on left, one on rihgt?

I am trying to draw a chart of tickdata that with askprice and tha sma of spread ="ask price - bid price",
cause spread and ask price is different in scale , so i want to draw a two yasix picture,
i did
but, the two yaxis label are both on left side
how can i Separate the yaxis?

fig = px.line(df, x="datetime", y=\['sma_spread', "sma_plus", "sma_minus"\])  
fig2 = px.scatter(df,x=df\['datetime'\], y=\['a1', "b1"\])  
fig2.update_traces(yaxis ="y2")

show(fig.add_traces(fig2.data))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2025-01-25 18:53:12
  • 您错过了最后一步。配置第二个yaxis update_layout(yaxis2 = {“叠加层”:“ y”,“ side”:“ right”})
  • 已经生成了一个数据框架,该数据框架与代码用于演示

完整MWE的

import pandas as pd
import numpy as np
import plotly.express as px

# simulate data
df = pd.concat(
    [
        pd.DataFrame(
            (
                np.random.uniform(4.5, 5, [3, 1])
                * (1 + np.random.normal(loc=0.001, scale=0.01, size=100)).cumprod()
            ).T,
            columns=["sma_spread", "sma_plus", "sma_minus"],
        ).assign(datetime=pd.date_range("1-jan-2022", periods=100)),
        pd.DataFrame(
            (
                np.random.uniform(15, 16, [2, 1])
                * (1 + np.random.normal(loc=0.001, scale=0.01, size=100)).cumprod()
            ).T,
            columns=["a1", "b1"],
        ),
    ],
    axis=1,
)

fig = px.line(df, x="datetime", y=['sma_spread', "sma_plus", "sma_minus"])  
fig2 = px.scatter(df,x=df['datetime'], y=['a1', "b1"])  
fig2.update_traces(yaxis ="y2")
# one more step, config second axis
fig.add_traces(fig2.data).update_layout(yaxis2={"overlaying":"y", "side":"right"})

结构相同

  • you have missed one final step. Configure the second yaxis update_layout(yaxis2={"overlaying":"y", "side":"right"})
  • have generated a data frame that is same structure that your code uses to demonstrate

full MWE

import pandas as pd
import numpy as np
import plotly.express as px

# simulate data
df = pd.concat(
    [
        pd.DataFrame(
            (
                np.random.uniform(4.5, 5, [3, 1])
                * (1 + np.random.normal(loc=0.001, scale=0.01, size=100)).cumprod()
            ).T,
            columns=["sma_spread", "sma_plus", "sma_minus"],
        ).assign(datetime=pd.date_range("1-jan-2022", periods=100)),
        pd.DataFrame(
            (
                np.random.uniform(15, 16, [2, 1])
                * (1 + np.random.normal(loc=0.001, scale=0.01, size=100)).cumprod()
            ).T,
            columns=["a1", "b1"],
        ),
    ],
    axis=1,
)

fig = px.line(df, x="datetime", y=['sma_spread', "sma_plus", "sma_minus"])  
fig2 = px.scatter(df,x=df['datetime'], y=['a1', "b1"])  
fig2.update_traces(yaxis ="y2")
# one more step, config second axis
fig.add_traces(fig2.data).update_layout(yaxis2={"overlaying":"y", "side":"right"})

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文