簡體   English   中英

如何在可移動圖形中移動/移動y軸?

[英]How can I move/shift the y-axis in plotly figures?

說我有以下情節:

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go

trace1 = go.Scatter(x=[1,2,3], y=[4,5,6], marker={'color': 'red', 'symbol': 104, 'size': 10}, 
                    mode="markers+lines",  text=["one","two","three"], name='1st Trace')
data=go.Data([trace1])
layout=go.Layout(xaxis={'title':'x1', 'showline':True}, yaxis={'title':'y1', 'showline':True}, height=380, width=380)
figure1=go.Figure(data=data,layout=layout)
init_notebook_mode(connected=True)
iplot(figure1, show_link=False)

在此處輸入圖片說明

我想將x點增加1,但同時y軸也會向右移動1個單位(即,x軸從2開始而不是從1開始):

figure1['data'][0]['x'] = (2,3,4)
iplot(figure1)

在此處輸入圖片說明

我想保留原始的軸布局(即,即使我增加了x軸的值,我仍然希望x軸從1開始而不是從2開始)。 我嘗試將tickvalsticktext添加到layoutxaxis參數中,但這似乎沒有任何效果:

figure1['layout'] = {'xaxis':{'title':'x2', 
                              'tickvals':[0,1,2,3,4,5], 
                              'ticktext':[0,1,2,3,4,5],
                              'showline':True
                             }, 
                     'yaxis':{'title':'y1', 'showline':True},
                     'height':380,
                     'width':380
                    }

iplot(figure1)

在此處輸入圖片說明

我也嘗試使用tickmode='linear'ticks='outside' ,但它們也沒有任何作用。

我該如何實現?

您可以通過手動設置x軸范圍來完成此操作:

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go

trace1 = go.Scatter(x=[1,2,3], y=[4,5,6], 
                    marker={'color': 'red', 'symbol': 104, 'size': 10}, 
                    mode="markers+lines",  
                    text=["one","two","three"], 
                    name='1st Trace')
data = go.Data([trace1])
layout = go.Layout(xaxis = {'range': [0.75,4.25],
                            'title':'x1', 
                            'showline':True}, 
                   yaxis = {'title':'y1', 
                            'showline':True}, 
                   height=380, width=380)

figure1 = go.Figure(data=data, layout=layout)
figure1['data'][0]['x'] = (2,3,4)
plot(figure1)

具有固定x軸范圍的圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM