使用Python Bokeh 创建具有多个字形的绘图
在本文中,我们将学习多个字形以及在散景中添加图例。现在散景为我们提供了多种可用于表示图中点的字形。一些字形是圆形、方形、星号、倒三角形()、三角形()等。
安装
该模块不是内置于Python的。要安装它,请在终端中键入以下命令。
pip install bokeh
散景中的多个字形
在这个例子中,我们将探索散景中不同类型的字形。下面实现中的每个字形都有不同的属性来标识自己。所以,让我们转向代码来理解这个概念。
代码:
Python3
# importing numpy package
import numpy as np
# importing figure and show from
# bokeh.plotting module
from bokeh.plotting import figure, show
# using numpy package to
# create a list of 5 numbers
x = np.arange(5)
# y is stored with the square
# of the numbers in x
y = x**2
# z is storing 3 times the value
# of the elements in x
z = x*3
# storing 7 numbers in p between
# 1 to 20
p = np.linspace(1,20,7)
# storing 7 numbers in q between
# 1 to 10
q = np.linspace(1, 10, 7)
# storing 5 numbers in r between
# 1 to 30
r = np.linspace(1, 30, 5)
# creating 31 elements in a list
a = np.arange(31)
# creating an empty figure with specific plot
# width and height
fig = figure(plot_width = 600 , plot_height = 600)
# plotting the points in the form of
# cicular glyphs
fig.circle(x, y, color = "red", size = 20)
# plotting the points in the form of
# square glyphs
fig.square(x, z, color = "blue", size = 15, alpha = 0.5)
# plotting the points in the form of
# hex glyphs
fig.hex(y, z, color = "green", size = 10, alpha = 0.7)
# drawing a line between the plotted points
fig.line(x, y, color = "green", line_width = 4)
# plotting the points in the form of
# inverted triangle glyph
fig.inverted_triangle(p, q, color = "yellow", size = 20, alpha = 0.4)
# plotting the points in the form of
# diamond glyphs
fig.diamond(x, r, color = "purple", size = 16, alpha = 0.8)
# plotting the points in the form of
# cross glyphs
fig.cross(a, a, size = 14)
# showing the above plot
show(fig)
Python3
# importing numpy as np
import numpy as np
# importing figure and show from
# bokeh.plotting
from bokeh.plotting import figure, show
# creating a list of numbers from 0-10
x = np.arange(11)
# Creating square of the numbers and
# storing it in y
y = np.linspace(1,5,11)
# Creating an array of random values in
# z
z = np.linspace(0,2,11)
# Creating a plot with plot width and height
# as 600
p = figure(plot_height = 600 , plot_width = 600)
# Plotting first line in the form of circle
p.circle(x, y, legend_label = "Circle", size = 30)
# Creating first line in the form of square
p.square(x, y*2, legend_label = "Square", size = 20, color = "green")
# Creating forth line in the form of line
p.inverted_triangle(x, y*3, legend_label = "Inverted Triangle",
size = 15, color = "purple")
# Increasing the glyph height
p.legend.glyph_height = 50
# increasing the glyph width
p.legend.glyph_width = 90
# showing the above plot
show(p)
Python3
# importing autompg dataset from bokeh.sampledata.autompg
from bokeh.sampledata.autompg import autompg
# importing columndatasource package from
# bokeh.models module
from bokeh.models import ColumnDataSource
# importing figure and show from
# bokeh.plotting module to plot the figure
from bokeh.plotting import figure, show
# provides data to the glyphs of the plot
source = ColumnDataSource(autompg)
# Creating an empty figure
p = figure(plot_height = 600, plot_width = 600)
# Creating circular glyphs with points plotted from
# columns taken from auto-mpg dataset
p.circle(x = 'hp', y = 'mpg', size = 20,
alpha = 0.6, source = autompg ,
legend_label = "Blue")
# Creating circular glyphs with points plotted from
# columns taken from auto-mpg dataset with different colors
p.circle(x = 'hp', y = 'displ', size = 20,
alpha = 0.6, color = "green" , source = autompg,
legend_label = "Green")
# Adjusting the label height using legend
p.legend.label_height = 50
# Adjusting the label_width using legend
p.legend.label_width = 50
# Adjusting the glyph width using legend
p.legend.glyph_width = 90
# Adjsting the glyph height using legend
p.legend.glyph_height = 90
# Showing the above plot
show(p)
输出:
解释:
- 首先,我们从不同的模块导入numpy 、 figure和show包。
- 现在,我们正在使用排列创建一系列数字并将列表存储在 x 中。
- 此后,我们还初始化了不同的变量,以便相互绘制图形。
- 初始化后,我们将创建一个空图形,绘图宽度和高度为 600。
- 由于散景为我们提供了不同类型的字形,我们在本示例中使用各种颜色和大小的点来实现其中的一些。形状为圆形的点集颜色为红色,大小为 20,而形状为六边形的点集为绿色,大小为 10,颜色的不透明度为 0.7(由alpha表示)。
具有散景图例的多个字形
现在,让我们转到本文的下一个主题,即散景图例。图例在散景图中起着非常重要的作用。它们帮助我们识别绘图中使用的不同类型的字形。除此之外,我们还可以更改可以区分不同字形的框的不同属性。
示例 1:
在此示例中,我们将探索多个字形以及散景图例以识别它们,并更改区分图的框的某些属性。这很重要,因为在具有各种类型字形的情节中,我们需要一些东西来识别它们,这就是散景图例发挥作用的时候。现在让我们转到代码实现。
代码:
蟒蛇3
# importing numpy as np
import numpy as np
# importing figure and show from
# bokeh.plotting
from bokeh.plotting import figure, show
# creating a list of numbers from 0-10
x = np.arange(11)
# Creating square of the numbers and
# storing it in y
y = np.linspace(1,5,11)
# Creating an array of random values in
# z
z = np.linspace(0,2,11)
# Creating a plot with plot width and height
# as 600
p = figure(plot_height = 600 , plot_width = 600)
# Plotting first line in the form of circle
p.circle(x, y, legend_label = "Circle", size = 30)
# Creating first line in the form of square
p.square(x, y*2, legend_label = "Square", size = 20, color = "green")
# Creating forth line in the form of line
p.inverted_triangle(x, y*3, legend_label = "Inverted Triangle",
size = 15, color = "purple")
# Increasing the glyph height
p.legend.glyph_height = 50
# increasing the glyph width
p.legend.glyph_width = 90
# showing the above plot
show(p)
输出:
解释:
- 我们正在创建三个不同的变量,其中包含一组相互绘制的值。
- 我们可以看到三个图,每个图都有不同的字形。根据代码,我们提供了大小为 30 的圆形字形、大小为 20 的方形字形和大小为 15 的倒三角形字形。此外,所有字形也是不同颜色的。
这个例子中的主要概念是,我们为图中的每个图提供了标签,并在散景中使用图例,我们正在改变右上角框中显示的字形的属性(由默认)。
示例 2:
在最后一个示例中,我们将数据集导入到我们的代码中。之后,我们将绘制不同的图形并使用散景图例,我们将分别更改标签大小和字形大小。让我们转向实施。
代码:
蟒蛇3
# importing autompg dataset from bokeh.sampledata.autompg
from bokeh.sampledata.autompg import autompg
# importing columndatasource package from
# bokeh.models module
from bokeh.models import ColumnDataSource
# importing figure and show from
# bokeh.plotting module to plot the figure
from bokeh.plotting import figure, show
# provides data to the glyphs of the plot
source = ColumnDataSource(autompg)
# Creating an empty figure
p = figure(plot_height = 600, plot_width = 600)
# Creating circular glyphs with points plotted from
# columns taken from auto-mpg dataset
p.circle(x = 'hp', y = 'mpg', size = 20,
alpha = 0.6, source = autompg ,
legend_label = "Blue")
# Creating circular glyphs with points plotted from
# columns taken from auto-mpg dataset with different colors
p.circle(x = 'hp', y = 'displ', size = 20,
alpha = 0.6, color = "green" , source = autompg,
legend_label = "Green")
# Adjusting the label height using legend
p.legend.label_height = 50
# Adjusting the label_width using legend
p.legend.label_width = 50
# Adjusting the glyph width using legend
p.legend.glyph_width = 90
# Adjsting the glyph height using legend
p.legend.glyph_height = 90
# Showing the above plot
show(p)
输出:
解释:
- 随着导入我们之前导入的所有包,这次我们导入了一个新的数据集auto-mpg 。与此同时,我们还导入了ColumnDataSource包。
- 使用 ColumnDataSouce 包,我们手动创建它,这有助于我们在多个图和小部件之间共享数据。
- 之后,我们将从 auto-mpg 数据集中获取的数据相互绘制。
- 然后使用散景的图例属性(figure.legend.label_width,figure.legend.glyph_width,figure.legend.label_height,figure.legend.glyph_height)我们正在更改用于识别图中绘图的标签和字形的大小。