I am trying to plot several ranges of my data into the same plot with plotly. For example the xaxis shall cover the ranges [0,14],[520,540] and [850,890]. However, when taking these data into one plot, there will be huge empty gaps inbetween the data regions, as the plot will simply cover the complete range from [0,890]. Due to this scaling the individual features of the data will be compresed to the point were nothing is discernible.
What I what to achieve is something like in this image:
Is it even possible to plot such a discontinued axis? If anybody also know how this discontinuation is called or if there is a name for that, I would be interested to hear it
Thanks to everyone
There's no direct way to do this with plotly. But with a little creativity you can easily make a setup that should come pretty close to what you're asking for. And save you from the pain of static matplotlib approaches. I'll spare you the details in case the following figure is not to your liking. But if it is, then I don't mind explaining the details. You'll find most of the details in the comments in the complete code snippet below though.
# 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()