您好,欢迎访问一九零五行业门户网

如何在Python Plotly中设置Y轴的范围?

plotly支持在x轴和y轴上设置范围。让我们了解如何在plotly中设置y轴的范围。
plotly.graph_objects is used to generate figures. it contains a lot of methods to customize charts and render a chart in html format.
create a numpy module and generate random ranges for both x and y axis.
创建figure()方法,以线条模式绘制x轴和y轴
create update_layout() method and set the y-axis range.
按照给定的步骤来设置plotly中y轴的范围。
第一步 - 导入plotly
导入 plotly.graph_objs 模块,并将其别名为 go
import plotly.graphs_objs as go
step 2 − import numpy
导入 numpy 模块并将其别名设置为 np,并设置随机 种子 值。
import numpy as npnp.random.seed(3)
step 3 − generate random number on x-axis
let us generate a list of random range of numbers on x-axis.
x = list(range(0,20,2))
第四步 - 在y轴上生成随机数
在y轴上生成随机数如下 -
y = np.random.randn(10)
step 5 − generate the scatter plot
let us generate the scatter plot with the following coordinates −
fig = go.figure(data=go.scatter(x=x, y=y, mode='lines'))
step 6 − set the y-axis range
使用update_layout()方法设置y轴范围。
fig.update_layout(yaxis_range=[-3,3])
步骤 7 - 显示图像
使用 show() 方法显示图表。
fig.show()
example 的中文翻译为:示例在python plotly中设置y轴范围的完整代码如下:
# importing librariesimport plotly.graph_objs as goimport numpy as npnp.random.seed(3)# generating numbers ranging from 0 to 18 on x-axisx = list(range(0,20,2))# generating random numbers on y-axisy = np.random.randn(10)# plotting scatter plot on x and y data with# 'lines' as modefig = go.figure(data=go.scatter(x=x, y=y, mode='lines'))# setting the y-axis range from -3 to 3fig.update_layout(yaxis_range=[-3,3])# to display the figure in the output screenfig.show()
output它将在浏览器上显示以下输出 −
以上就是如何在python plotly中设置y轴的范围?的详细内容。
其它类似信息

推荐信息