用 2 个 y 轴绘制单迹线

Rap*_*oth 4 python plotly

我有一个绘图对象条形图,我想为其显示 2 个 y 轴(不同的货币,因此转换因子是常数)。

目前,我每条轨迹绘制 1 条,而对于第二条轨迹,我将不透明度设置为 0,禁用图例和悬停信息。这个 hack 有效,但维护起来很难。

我知道https://plotly.com/python/multiple-axes/

我当前的解决方案如下所示

import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots

# make up some data
dates = pd.DataFrame(pd.date_range('1/1/2023','1/7/2023'), columns=['date'])
dates["key"] = 0
items = pd.DataFrame(["A","B","C"], columns=['items'])
items["key"] = 0
df = dates.merge(items,on="key",how="outer").drop("key",axis=1)
df['price_USD'] = np.random.randint(0, 100, df.shape[0])
df['price_EURO'] = df['price_USD']/1.5

fig = make_subplots(specs=[[{"secondary_y": True}]])  
for item, _df in df.groupby("items",sort=True):
    ## we may set the colors of the individual items manually here
    fig.add_trace(
                go.Bar(
                    x=_df["date"],
                    y=_df["price_USD"],
                    showlegend=True,
                    name=item,
                    opacity=1.0,
                    #color=color,
                    legendgroup=item

                ),
                secondary_y=False,
            )

    # invisible trace
    fig.add_trace(
                go.Bar(
                    x=_df["date"],
                    y=_df["price_EURO"],
                    showlegend=False,
                    opacity=0.0,
                    name="",
                    hoverinfo="skip",
                    legendgroup=item
                ),
                secondary_y=True,
            )

fig.update_layout(barmode="stack")
fig.update_yaxes(title_text="<b>Cost USD", secondary_y=False)
fig.update_yaxes(title_text="<b>Cost Euro", showgrid=False, secondary_y=True)
fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有没有更干净的方法来做到这一点?

Dar*_*aan 5

可以使用另一侧作为scaleanchor并提供缩放。这样就不再需要将数据加倍了。

import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
np.random.seed(1)

RATIO = 1.5

# make up some data
dates = pd.DataFrame(pd.date_range('1/1/2023','1/7/2023'), columns=['date'])
dates["key"] = 0
items = pd.DataFrame(["A","B","C"], columns=['items'])
items["key"] = 0
df = dates.merge(items,on="key",how="outer").drop("key",axis=1)
df['price_USD'] = np.random.randint(0, 100, df.shape[0])

# This is not needed more, at least for displaying it
#df['price_EURO'] = df['price_USD']/ RATIO

fig = make_subplots(specs=[[{"secondary_y": True}]])  
for item, _df in df.groupby("items",sort=True):
    ## we may set the colors of the individual items manually here
    fig.add_trace(
                go.Bar(
                    x=_df["date"],
                    y=_df["price_USD"],
                    showlegend=True,
                    name=item,
                    opacity=1.0,
                    #color=color,
                    legendgroup=item

                ),
                secondary_y=False,

            )
 
# Add a dummy trace to activate the second axis  
fig.add_trace(
            go.Bar(visible=False),
            secondary_y=True,
        )
fig.update_layout(barmode="stack")
fig.update_yaxes(title_text="<b>Cost USD", secondary_y=False)
fig.update_yaxes(title_text="<b>Cost Euro", showgrid=False, secondary_y=True)
# Set scale according to the other axis and the defined ratio
# the constraint assures that it is bottom aligned
fig.update_layout(yaxis2=dict(scaleanchor="y1", scaleratio=RATIO, constraintoward="bottom"))
fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述