plotly:如何显示断轴

问题描述 投票:0回答:1

我需要在我的情节中描绘一个“断轴”,因为有些值很大。例如考虑:

import plotly.graph_objects as go
import numpy as np
y=np.arange(20)
y[0]=1000
go.Figure(go.Bar(
    y=y
))

This question 对 matplotlib 进行了相同的讨论,但我需要使用 plotly。

这是我想要创建的示例。

python plotly axis-labels
1个回答
3
投票

克服这个问题的一种方法是有两个子框架如下:

import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

y = np.arange(20)
y[0] = 1000

color = px.colors.sequential.Plasma[0]
cut_interval = [30, 700]
bar = go.Bar(
    y=y,
    name="foo",
    marker_color=color,
)
fig = make_subplots(
    rows=2,
    cols=1,
    vertical_spacing=0.05,
    shared_xaxes=True,
)

fig.append_trace(bar, row=1, col=1)
bar.showlegend = False
fig.append_trace(bar, row=2, col=1)

fig.update_yaxes(range=[cut_interval[1], max(y) * 1.1], row=1, col=1)
fig.update_xaxes(visible=False, row=1, col=1)
fig.update_yaxes(range=[0, cut_interval[0]], row=2, col=1)

这将导致:

两个子框架连接在x轴上,所以可以平移它们。

© www.soinside.com 2019 - 2024. All rights reserved.