Home > Blockchain >  Plotly Spikes Not Appearing on Subplots
Plotly Spikes Not Appearing on Subplots

Time:10-28

I am making a figure with multiple subplots. I want each subplot to show spikes but am unable to get the spikes showing on anything other that the first subplot. I didn't see that ability to set showspikes with a fig.update_traces call. Any suggestions?

Code to reproduce:

import plotly.graph_objs as go
from plotly.subplots import make_subplots


fig = make_subplots(rows=1, cols=2)

x1 = list(range(100))
y1 = [val**2 for val in x1]

x2 = list(range(150, 250))
y2 = [1./val for val in x2]

fig.add_trace(go.Scatter(x=x1, y=y1), row=1, col=1)
fig.add_trace(go.Scatter(x=x2, y=y2), row=1, col=2)

fig.update_layout(xaxis=dict(showspikes=True))

fig.show()

CodePudding user response:

  • make sure you set showspikes on each of the axes. Your figure contains xaxis and xaxis2
  • code below uses a dict comprehension to update all if the axes
import plotly.graph_objs as go
from plotly.subplots import make_subplots


fig = make_subplots(rows=1, cols=2)

x1 = list(range(100))
y1 = [val**2 for val in x1]

x2 = list(range(150, 250))
y2 = [1./val for val in x2]

fig.add_trace(go.Scatter(x=x1, y=y1), row=1, col=1)
fig.add_trace(go.Scatter(x=x2, y=y2), row=1, col=2)

fig.update_layout({ax:{"showspikes":True} for ax in fig.to_dict()["layout"] if ax[0:3]=="xax"})

CodePudding user response:

Rob's answer probably works, but you can achieve the same with:

fig.update_xaxes(showspikes = True) 
  • Related