gpt4 book ai didi

python - 如何设置y轴的范围

转载 作者:行者123 更新时间:2023-12-01 08:01:43 27 4
gpt4 key购买 nike

我有以下代码来使用 Plotly 创建线图。如何设置 Y 轴的范围使其始终位于 [0; 10]?

layout = go.Layout(
title=go.layout.Title(
text="Test",
xref='paper',
x=0
),
xaxis=go.layout.XAxis(
tickmode='linear',
tickfont=dict(
size=10
),
title=go.layout.xaxis.Title(
font=dict(
size=14,
color='#7f7f7f'
)
)
),
yaxis=go.layout.YAxis(
title=go.layout.yaxis.Title(
text=y,
font=dict(
size=14,
color='#7f7f7f'
)
)
)
)

data = [go.Scatter(x=x1, y=y1)]

最佳答案

更新新版本

设置图形时,您可以使用plotly的 magic underscore notation并指定layout_yaxis_range=[<from_value>, <to_value>]像这样:

fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'), layout_yaxis_range=[-4,4])

或者,如果您已经有一个名为 fig 的人物,您可以使用:

fig.update_layout(yaxis_range=[-4,4])

还有:

fig.update(layout_yaxis_range = [-4,4])

或者:

fig.update_yaxes(range = [-4,4])

图:

enter image description here

完整代码:

# imports
import pandas as pd
import plotly.graph_objs as go
import numpy as np

# data
np.random.seed(4)
x = np.linspace(0, 1, 50)
y = np.cumsum(np.random.randn(50))

# plotly line chart
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'), layout_yaxis_range=[-4,4])

fig.update_layout(yaxis_range=[-4,4])
fig.show()
<小时/>

原始答案使用 plotly.offline , iplot并且没有神奇的下划线符号:

设置图形时,使用:

layout = go.Layout(yaxis=dict(range=[fromValue, toValue])

或者,如果您已经有一个名为 fig 的人物,您可以使用:

fig.update_layout(yaxis=dict(range=[fromValue,toValue]))

plotly :

enter image description here

Jupyter Notebook 的完整代码:

# imports
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import plotly.graph_objs as go
import numpy as np

# setup
init_notebook_mode(connected=True)

# data
np.random.seed(4)
x = np.linspace(0, 1, 50)
y = np.cumsum(np.random.randn(50))

# line
trace = go.Scatter(
x=x,
y=y,
)

# layout
layout = go.Layout(yaxis=dict(range=[-4,4])
)

# Plot
fig = go.Figure(data=[trace], layout=layout)
iplot(fig)

一些重要细节:

通过此设置,您可以轻松添加 y 轴标题,如下所示:

# layout
layout = go.Layout(yaxis=dict(range=[-4,4]), title='y Axis')
)

如果您想进一步设置该标题的格式,那就有点棘手了。我发现使用 title = go.layout.yaxis.Title(text='y Axis', font=dict(size=14, color='#7f7f7f') 添加另一个元素最简单。只要你的方法正确,你就不会遇到上面评论中的情况:

Thanks. I tried it. But then I have 2 definitions of yaxis in theLayout: yaxis=dict(range=[0, 10]) and yaxis=go.layout.YAxis. Thereforean error appears.

看看这个:

plotly :

enter image description here

具有 y 轴文本格式的完整代码:

# imports
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import plotly.graph_objs as go
import numpy as np

# setup
init_notebook_mode(connected=True)

# data
np.random.seed(4)
x = np.linspace(0, 1, 50)
y = np.cumsum(np.random.randn(50))

# line
trace = go.Scatter(
x=x,
y=y,
)

# layout
layout = go.Layout(
yaxis=dict(range=[-4,4],
title = go.layout.yaxis.Title(text='y Axis', font=dict(size=14, color='#7f7f7f')))
)

# Plot
fig = go.Figure(data=[trace], layout=layout)
iplot(fig)

关于python - 如何设置y轴的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55704058/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com