5
import matplotlib.pyplot as plt
import numpy as np
import plotly.plotly as py
from plotly.graph_objs import *

py.sign_in('uname', 'pass')


trace1 = Scatter(
    x=[1,2,3,4,5,6,7,8],
    y=[24,25,30,21,33,31,30,29],
    mode='lines',
    xaxis='x1',
    )

layout = Layout(
    title="My first plot",
    yaxis=YAxis(
        title = "y1"
        ),
    xaxis=XAxis(
        title= 'x1',
        anchor = 'x2'
        ),
    xaxis2=XAxis(
        title= 'x2',
        side = 'top',
        overlaying = 'y'
        ),
    )

data = [trace1]

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

plot_url = py.plot(fig)

我正在尝试在图的顶部创建第二个 X 轴(我们称之为 x2)。我希望它使用公式 x2=x1*0.3 链接到 x1 值。在 matplotlib 中,我只需定义另一个轴并重新定义其范围,即使我放大/缩小比例也会保持不变:

ax2 = ax1.twiny()
start, end = ax1.get_xlim()
ax2.set_xlim(start*0.3, end*0.3)

所以效果应该是这样的:matplotlib 中的缩放轴

如何在情节中达到相同的效果?

4

1 回答 1

3

很接近了!这是 Plotly 中多个 x 轴的简单示例,改编自Plotly with Python 中的多个 y 轴示例

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')

创建此图:(具有多个 x 轴的图形交互式版本:https ://plot.ly/~chris/3285 )

请注意,您可以在各个轴上进行缩放和平移:具有多个 x 轴的 Plotly 图的交互性

Range您可以使用参数手动指定这些轴的范围,这将在您使用滚动放大和缩小时保持比例。这是一个简单的例子:

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')

这是图表

于 2014-12-06T19:20:26.990 回答