简体   繁体   中英

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

Say I have the following plot:

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)

在此处输入图片说明

I want to increment the x-ticks by 1, but while doing that the y-axis also moves 1 unit to the right (ie, the x-axis starts at 2 now instead of 1):

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

在此处输入图片说明

I want to retain the original axis layout (ie, I still want the x-axis to start at 1, and not at 2, even though I incremented the x-axis values). I tried adding tickvals and ticktext to the xaxis parameter in layout , but that doesn't seem to have any effect:

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)

在此处输入图片说明

I also tried using tickmode='linear' and ticks='outside' , but they don't have any effect either.

How can I achieve this?

You can accomplish this by manually setting the x axis range:

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轴范围的图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM