In plotly, how do I create a linked X axis?

旧巷老猫 提交于 2019-12-06 07:54:45

Pretty close! Here is a simple example of multiple x-axes in Plotly, adapted from this example of multiple y-axes in Plotly with Python

import plotly.plotly as py
from plotly.graph_objs import *

trace1 = Scatter(
    x=[1,2,3],
    y=[24,30,25],
    mode='lines',
    xaxis='x1',
)

trace2 = Scatter(
    x=[10,20,30],
    y=[24,25,30],
    mode='lines',
    xaxis='x2',
)

layout = Layout(
    title="Multiple X-Axes",
    yaxis=YAxis(
        title = "y1"
        ),
    xaxis=XAxis(
        title= 'x-axis 1'
    ),
    xaxis2=XAxis(
        title= 'x-axis 2',
        side = 'top',
        overlaying='x1'
    )
)

data = [trace1, trace2]

fig = Figure(data=data, layout=layout)

py.plot(fig, filename='multiple x axes')

Which creates this graph:

(Interactive version: https://plot.ly/~chris/3285)

Note that you can zoom and pan on the individual axes:

You can specify the range of these axes manually, with the Range parameter, which would maintain the ratio as you zoom in and out with scroll. Here is a simple example:

import plotly.plotly as py
from plotly.graph_objs import *

trace1 = Scatter(
    x=[1,2,3],
    y=[24,30,25],
    mode='lines',
    xaxis='x1',
)

trace2 = Scatter(
    x=[10,20,30],
    y=[24,25,30],
    mode='lines',
    xaxis='x2',
)

layout = Layout(
    title="Multiple X-Axes",
    yaxis=YAxis(
        title = "y1"
        ),
    xaxis=XAxis(
        title= 'x-axis 1',
        range=[1, 3]
    ),
    xaxis2=XAxis(
        title= 'x-axis 2',
        side = 'top',
        overlaying='x1',
        range=[10, 30]
    )
)

data = [trace1, trace2]

fig = Figure(data=data, layout=layout)

py.plot(fig, filename='multiple x axes with custom range')

And here is the graph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!