在 plotly 中,如何创建链接的 X 轴? [英] In plotly, how do I create a linked X axis?
问题描述
将 matplotlib.pyplot 导入为 plt将 numpy 导入为 np导入 plotly.plotly 作为 py从 plotly.graph_objs 导入 *py.sign_in('uname', 'pass')trace1 = 散点(x=[1,2,3,4,5,6,7,8],y=[24,25,30,21,33,31,30,29],模式='线',xaxis='x1',)布局 = 布局(title="我的第一个情节",yaxis=yaxis(标题 = "y1"),x轴=X轴(标题 = 'x1',锚='x2'),xaxis2=X轴(标题 = 'x2',边 = '顶',覆盖 = 'y'),)数据 = [trace1]fig = 图(数据=数据,布局=布局)plot_url = py.plot(fig)
我正在尝试在图的顶部创建第二个 X 轴(我们称之为 x2).我希望它使用公式 x2=x1*0.3 链接到 x1 值.在 matplotlib 中,我会简单地定义另一个轴并重新定义它的范围,即使我放大/缩小比例也会保持不变:
ax2 = ax1.twiny()开始,结束 = ax1.get_xlim()ax2.set_xlim(开始*0.3,结束*0.3)
所以效果应该是这样的:
如何在plotly中达到同样的效果?
非常接近!这是 Plotly 中多个 x 轴的简单示例,改编自 )
请注意,您可以在各个轴上缩放和平移:
您可以手动指定这些轴的范围,使用 Range
参数,它会在您滚动放大和缩小时保持比例.这是一个简单的例子:
import plotly.plotly as py从 plotly.graph_objs 导入 *trace1 = 散点(x=[1,2,3],y=[24,30,25],模式='线',xaxis='x1',)trace2 = 散点(x=[10,20,30],y=[24,25,30],模式='线',xaxis='x2',)布局 = 布局(title="多个 X 轴",yaxis=yaxis(标题 = "y1"),x轴=X轴(title='x 轴 1',范围=[1, 3]),xaxis2=XAxis(title='x 轴 2',边 = '顶',叠加='x1',范围=[10, 30]))数据 = [trace1, trace2]fig = 图(数据=数据,布局=布局)py.plot(fig, filename='multiple x axes with custom range')
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)
I am trying to create a second X axis on the top of the plot (let's call it x2). I want it to be linked to x1 values with a formula x2=x1*0.3. In matplotlib, I would simply define another axis and redefine its range, and the ratio would be maintained even if I zoom in/out:
ax2 = ax1.twiny()
start, end = ax1.get_xlim()
ax2.set_xlim(start*0.3, end*0.3)
So the effect should look like this:
How do I achieve the same effect in plotly?
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')
这篇关于在 plotly 中,如何创建链接的 X 轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!