绘制 discontinued/broken 轴,x 轴上有间隙表示刻度跳跃

Plot discontinued/broken axis with gap in the x axis indicating scale jump

我正在尝试使用 plotly 将我的多个数据范围绘制到同一个图中。例如,x 轴应覆盖范围 [0,14]、[520,540] 和 [850,890]。然而,当将这些数据放入一个图中时,数据区域之间将存在巨大的空白,因为该图将简单地覆盖 [0,890] 的完整范围。由于这种缩放,数据的各个特征将被压缩到无法辨认任何东西的程度。

我要实现的是这张图片中的内容:

甚至可以绘制这样一个已停产的轴吗?如果有人也知道这个停产是怎么叫的,或者有一个名字,我很想听听

感谢大家

没有直接 方法可以用 plotly 做到这一点。但是只要有一点创意,您就可以轻松地进行设置,使其非常接近您的要求。 并且 让您免于静态 matplotlib 方法的痛苦。如果您不喜欢下图,我将为您提供详细信息。但如果是,那么我不介意解释细节。不过,您可以在下面完整代码段的注释中找到大部分详细信息。

地块 1:

代码 1:

# imports
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
from plotly.subplots import make_subplots

# data
df1 = pd.DataFrame({'years': [1995, 1996, 1997, 1998, 1999, 2000,],
                  'China': [219, 146, 112, 127, 124, 180],
                  'Rest of world': [16, 13, 10, 11, 28, 37]}).set_index('years')

df2 = pd.DataFrame({'years': [2008, 2009, 2010, 2011, 2012, 2013,],
                  'China': [207, 236, 263,350, 430, 474],
                  'Rest of world': [43, 55, 56, 88, 105, 156]}).set_index('years')

df3 = pd.DataFrame({'years': [2017, 2018, 2019, 2020, 2021, 2022],
                  'China': [488, 537, 500, 439, 444, 555],
                  'Rest of world': [299, 340, 403, 549, 300, 311]}).set_index('years')
# df.set_index('years', inplace = True)

# organize datafames with different x-axes in a dict
dfs = {'df1': df1,
       'df2': df2,
       'df3': df3}

# subplot setup
colors = px.colors.qualitative.Plotly
fig = make_subplots(rows=1, cols=len(dfs.keys()), horizontal_spacing = 0.02)
fig.update_layout(title = "Broken / discontinued / gapped x-axis")

# Assign columns from dataframes in dict to the correct subplot
for i, dfd in enumerate(dfs, start =1):
    for j, col in enumerate(dfs[dfd].columns):
        fig.add_trace(go.Scatter(x=dfs[dfd].index,
                        y=dfs[dfd][col],
                        name=col,
                        marker_color=colors[j],
                        legendgroup = col,
                        showlegend = True if i == 1 else False,
                        ), row=1, col=i)

# this section is made specifically for this dataset
# and this number of dataframes but can easily
# be made flexible wrt your data if this setup
# if something you can use
fig.update_yaxes(range=[0, 750])
fig.update_yaxes(showticklabels=False, row=1, col=2)
fig.update_yaxes(showticklabels=False, row=1, col=3)

# and just a little aesthetic adjustment
# that's admittedly a bit more challenging
# to automate...
# But entirely possible =D
fig.add_shape(type="line",
    x0=0.31, y0=-0.01, x1=0.33, y1=0.01,
    line=dict(color="grey",width=1),
    xref = 'paper',
    yref = 'paper'
)

fig.add_shape(type="line",
    x0=0.32, y0=-0.01, x1=0.34, y1=0.01,
    line=dict(color="grey",width=1),
    xref = 'paper',
    yref = 'paper'
)

fig.add_shape(type="line",
    x0=0.66, y0=-0.01, x1=0.68, y1=0.01,
    line=dict(color="grey",width=1),
    xref = 'paper',
    yref = 'paper'
)

fig.add_shape(type="line",
    x0=0.67, y0=-0.01, x1=0.69, y1=0.01,
    line=dict(color="grey",width=1),
    xref = 'paper',
    yref = 'paper'
)

fig.update_layout(template='plotly_white')
fig.show()