简体   繁体   English

如何在可移动图形中移动/移动y轴?

[英]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): 我想将x点增加1,但同时y轴也会向右移动1个单位(即,x轴从2开始而不是从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). 我想保留原始的轴布局(即,即使我增加了x轴的值,我仍然希望x轴从1开始而不是从2开始)。 I tried adding tickvals and ticktext to the xaxis parameter in layout , but that doesn't seem to have any effect: 我尝试将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)

在此处输入图片说明

I also tried using tickmode='linear' and ticks='outside' , but they don't have any effect either. 我也尝试使用tickmode='linear'ticks='outside' ,但它们也没有任何作用。

How can I achieve this? 我该如何实现?

You can accomplish this by manually setting the x axis range: 您可以通过手动设置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.

相关问题 Dash/Plotly:如何使用相同的 x 轴在 y 轴上绘制两列? - Dash/Plotly: How can I plot two columns on the y-axis with the same x axis? Plotly 如何将x轴移动到顶部和y轴向右移动? - Plotly How to move x-axis to top and y-axis to the right? 如何使用下拉菜单更新多个图形的y轴列? - How to update y-axis column for multiple figures using dropdown? Plotly:如何使用 plotly 表示在辅助 y 轴上的 plot - Plotly: How to plot on secondary y-Axis with plotly express 在 plotly 快递中,如何重命名轴和我的图例名称? y 轴仅显示为值,图例上的标题是可变的 - In plotly express, how can I rename the axes and the name of my legend? The y-axis is only showing as values and the title on the legend is variable 当 plotly 图表中的 x 轴发生变化时,如何自动调整 y 轴? - How do I auto-adjust the y-axis when the x-axis changes in a plotly chart? 如何更改 plotly 中的 x 轴和 y 轴标签? - How to change the x-axis and y-axis labels in plotly? 如何在谷歌表中添加第二个 y 轴 - How can i add a second y-axis in google sheet 如何通过更新菜单更新 Plotly 图表的次 Y 轴 - How can one update the secondary Y-axis of a Plotly chart through an update menu 如何在 pandas 子图上生成次要 y 轴? - How can I generate secondary y-axis on pandas subplots?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM