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')
layout=go.Layout(xaxis={'title':'x1', 'showline':True}, yaxis={'title':'y1', 'showline':True}, height=380, width=380)
iplot(figure1, show_link=False)

enter image description here

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

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

enter image description here

I want to retain the original axis layout (i.e., 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', 
                     'yaxis':{'title':'y1', 'showline':True},


enter image description here

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

How can I achieve this?


Hi @kristada619,

I don’t think I understand what you want to achieve. Are you saying that you want to specify the x data as [1, 2, 3] but you want the x tick labels to read [2, 3, 4]? If so you could do this by setting tickvals=[1, 2, 3] and ticktext=[2, 3, 4], but that seems a bit misleading.

Can you include an image, even from another library, of what you want the end result to look like?



Hi @jmmease,

I figured out the solution. After modifying the x-tick values, we need to give the x-axis the range of values in the x-axis to display; otherwise, it starts from the smallest x-tick value by default. So, the following is what I needed:

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

x_min = 1
x_max = 4.5

figure1['layout']['xaxis'].update(range=[x_min, x_max])