Python 绘制中断/断开的轴,x轴上的间隙表示缩放跳跃

Python 绘制中断/断开的轴,x轴上的间隙表示缩放跳跃,python,plotly,Python,Plotly,我正在尝试使用plotly将多个数据区域绘制到同一个绘图中。例如,xaxis应涵盖范围[0,14]、[520540]和[850890]。然而,当将这些数据放入一个图中时,数据区域之间会有巨大的空白,因为该图仅涵盖[0890]的整个范围。由于这种缩放,数据的各个特征将被压缩到无法识别的程度 我所要达到的目标如下所示: 甚至有可能画出这样一个不连续的轴吗?如果有人也知道这种中止是如何被称为的,或者是否有一个名字,我很想听听 多亏了所有人没有直接的方法可以用plotly做到这一点。但是只要有一点创意

我正在尝试使用plotly将多个数据区域绘制到同一个绘图中。例如,xaxis应涵盖范围[0,14]、[520540]和[850890]。然而,当将这些数据放入一个图中时,数据区域之间会有巨大的空白,因为该图仅涵盖[0890]的整个范围。由于这种缩放,数据的各个特征将被压缩到无法识别的程度

我所要达到的目标如下所示:

甚至有可能画出这样一个不连续的轴吗?如果有人也知道这种中止是如何被称为的,或者是否有一个名字,我很想听听

多亏了所有人

没有直接的方法可以用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()