📜  如何设置轴限制散景?

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

如何设置轴限制散景?

在本文中,我们将学习如何在散景中为绘图设置轴限制。当我们用一组值绘制图形时,会自动创建点的 X 限制和 Y 限制。但是散景允许我们根据自己的选择设置这些轴限制。

因此,首先,我们需要知道为了将 Axis 限制设置为 X 和 Y 轴,我们需要从我们的bokeh.models模块中导入一个包,称为 range1d 。 Range1d 使我们能够为我们选择的绘图设置轴限制。

我们可以使用 google colab,也可以使用本地设备中的任何文本编辑器来完成上述实现。为了在我们的本地设备上使用文本编辑器,我们首先需要打开命令提示符并编写以下代码。

pip install bokeh

安装完成后,现在我们准备进行主要实现。

在下面的代码中,我们根据我们的选择创建了一个带有 X 轴和 Y 轴限制的空图。



代码:

Python3
# importing figure and show from
# bokeh.plotting
from bokeh.plotting import figure, show
  
# importing range1d from
# bokeh.models in order to change
# the X-Axis and Y-Axis ranges
from bokeh.models import Range1d
  
# Determining the plot height
# and plot width
fig = figure(plot_width=500, plot_height=500)
  
# With the help of x_range and
# y_range functions we are setting
# up the range of both the axis
fig.x_range = Range1d(0, 10)
fig.y_range = Range1d(5, 15)
  
# Thus an empty plot is created
# where the ranges of both the
# axes are custom set by us.
show(fig)


Python3
# importing figure and show from
# bokeh.plotting
from bokeh.plotting import figure, show
  
# importing range1d from
# bokeh.models in order to change
# the X-Axis and Y-Axis ranges
from bokeh.models import Range1d
  
# Determining the plot height
# and plot width
fig = figure(plot_width=620,
             plot_height=600)
  
# With the help of x_range and
# y_range functions we are setting
# up the range of both the axis
fig.x_range = Range1d(20, 25)
fig.y_range = Range1d(35, 100)
  
# Creating two graphs in a
# single figure where we are
# plotting the points in the range
# set by us
fig.line([21, 20, 23, 24, 22],
         [36, 72, 51, 90, 56],
         line_width=2, color="green")
  
fig.circle([21, 20, 23, 24, 22],
           [36, 72, 51, 90, 56],
           size=20, color="green",
           alpha=0.5)
  
# Showing the plot
show(fig)


输出:

解释:

从代码中我们可以清楚地看到,我们将 X 轴限制设置为 0-10,将 Y 轴限制设置为 5-15。这件事已经在上图中实现了。

示例 2:

在第二个示例中,我们正在设置我们自己的 X 轴和 Y 轴限制,然后我们指向图形中该范围内的一组点。下面的代码显示了上面的实现。



蟒蛇3

# importing figure and show from
# bokeh.plotting
from bokeh.plotting import figure, show
  
# importing range1d from
# bokeh.models in order to change
# the X-Axis and Y-Axis ranges
from bokeh.models import Range1d
  
# Determining the plot height
# and plot width
fig = figure(plot_width=620,
             plot_height=600)
  
# With the help of x_range and
# y_range functions we are setting
# up the range of both the axis
fig.x_range = Range1d(20, 25)
fig.y_range = Range1d(35, 100)
  
# Creating two graphs in a
# single figure where we are
# plotting the points in the range
# set by us
fig.line([21, 20, 23, 24, 22],
         [36, 72, 51, 90, 56],
         line_width=2, color="green")
  
fig.circle([21, 20, 23, 24, 22],
           [36, 72, 51, 90, 56],
           size=20, color="green",
           alpha=0.5)
  
# Showing the plot
show(fig)

输出:

解释:

由于这是我们定制的轴,图形的大小将完全取决于轴的限制。例如:如果两个轴的轴限制都较大,则上面绘制的图形将小于现在显示的图形。