📜  Python Plotly:如何设置 y 轴的范围?

📅  最后修改于: 2022-05-13 01:55:30.174000             🧑  作者: Mango

Python Plotly:如何设置 y 轴的范围?

在本文中,我们将学习如何在Python中使用 plotly 设置图形的 y 轴范围。

要安装此模块,请在终端中键入以下命令:

pip install plotly

示例 1:使用 layout_yaxis_range 作为参数

在这个例子中,我们首先导入了所需的库,即pandas、numpyplotly.objs,然后我们生成了一些在 x 轴和 y 轴上绘制的数字列表,此外,我们使用了 go.Scatter()函数制作散点图。 go.Figure()函数将数据作为输入,我们使用mode='lines ' 将模式设置为' lines' 。我们使用了神奇的下划线符号,即layout_yaxis_range=[-8,8]来设置 y 轴范围从 -8 到 8。最后我们使用show()函数显示图形。

Python3
# Importing Libraries
import pandas as pd
import plotly.graph_objs as go
import numpy as np
 
# generating numbers ranging from 1 to 20
# on x-axis
x = list(range(1,20))
 
# generating random numbers on y-axis
y = np.random.randn(20)
 
# plotting scatter plot on x and y data with 'lines'
# as mode and setting the y-axis range from -8 to 8
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'),
                layout_yaxis_range=[-8,8])
 
# to display the figure in the output screen
fig.show()


Python3
# Importing Libraries
import pandas as pd
import plotly.graph_objs as go
import numpy as np
 
np.random.seed(5)
 
# generating numbers ranging from 1 to 20
# on x-axis
x = list(range(1,20))
 
# generating random numbers on y-axis
y = np.random.randn(20)
 
# plotting scatter plot on x and y data with
# 'lines' as mode
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
 
# setting the y-axis range from -3 to 3
fig.update_layout(yaxis_range=[-3,3])
 
# to display the figure in the output screen
fig.show()


Python3
# Importing Libraries
import numpy as np
import pandas as pd
import plotly.graph_objs as go
 
np.random.seed(5)
 
# generating numbers ranging from 1 to 20
# on x-axis
x = list(range(1,20))
 
# generating random numbers on y-axis
y = np.random.randn(20)
 
# plotting scatter plot on x and y data with
# 'lines' as mode
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
 
# and setting the y-axis range from -4 to 4
fig.update_layout(yaxis=dict(range=[-4,4]))
 
# to display the figure in the output screen
fig.show()


输出:

示例 2:稍后使用 update_layout()函数设置 y 轴范围

在下面的例子中,这里我们首先没有设置 y 轴范围来构建绘图。此外,我们使用update_layout()函数即fig.update_layout(yaxis_range=[-3,3])来设置 y 轴范围将范围设置为 -3 到 3。

Python3

# Importing Libraries
import pandas as pd
import plotly.graph_objs as go
import numpy as np
 
np.random.seed(5)
 
# generating numbers ranging from 1 to 20
# on x-axis
x = list(range(1,20))
 
# generating random numbers on y-axis
y = np.random.randn(20)
 
# plotting scatter plot on x and y data with
# 'lines' as mode
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
 
# setting the y-axis range from -3 to 3
fig.update_layout(yaxis_range=[-3,3])
 
# to display the figure in the output screen
fig.show()

输出:

示例 3:

同样,这里我们将dict(range=[-4,4])作为数字字典传递给update_layout()函数内的参数yaxis

Python3

# Importing Libraries
import numpy as np
import pandas as pd
import plotly.graph_objs as go
 
np.random.seed(5)
 
# generating numbers ranging from 1 to 20
# on x-axis
x = list(range(1,20))
 
# generating random numbers on y-axis
y = np.random.randn(20)
 
# plotting scatter plot on x and y data with
# 'lines' as mode
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
 
# and setting the y-axis range from -4 to 4
fig.update_layout(yaxis=dict(range=[-4,4]))
 
# to display the figure in the output screen
fig.show()

输出: