如何在散景中添加彩条?
Bokeh是最近很有前途的Python库之一。它在数据可视化方面提供了高性能和高效率。散景的一大优势是我们可以获得各种格式的输出文件,例如 HTML、笔记本等。在本文中,我们将学习如何在散景中添加色条。
现在,bokeh 为我们提供了各种可视化界面,例如bokeh.models、bokeh.plotting、bokeh.transform等,以便我们可以从中导入各种模块并使用它们来创建和定义绘图中的各种属性。
可以使用预装了所有内容的 Google colab,但是如果我们使用的是本地设备,那么在我们的设备中安装散景非常重要,否则这些功能将无法正常工作。首先,我们需要在桌面上打开一个命令提示符,然后转到我们要安装 bokeh 的以下目录。确保你已经在你的本地设备上安装了 node.js,否则你可以参考这个网站下载。进入目录后,在命令提示符下写入以下代码。
pip install bokeh
现在,我们可以使用任何文本编辑器来实现上述概念。
代码:
Python3
# importing numpy package
import numpy as np
# importing figure and show for creating
# and showing plots from bokeh.plotting
# interface
from bokeh.plotting import figure, show
# importing LinearColorMapper and ColorBar
# from bokeh.models to create color bars
from bokeh.models import LinearColorMapper, ColorBar
# Creating a 2D array of scalar data to plot
d = [[0.11023,0.97382,0.5634],
[0.66382,0.2484,0.36524],
[0.6845,0.9824,0.15498]]
# Using Linear color mapping, we are deciding
# palette color of the color bar and we are specifying
# low and high determining the range of values in color map
color = LinearColorMapper(palette = "Cividis256",
low = 0, high = 1)
# Creating a figure where we define
# the x-Axis limits and y-Axis limits
# of the plot
plot = figure(x_range = (0, 1),
y_range = (0,1))
# Creating the image of the plot by
# specifying width and height of the inside plot
# along with color mapper and scalar data d
plot.image(image = [d], color_mapper = color,
dh = [1], dw = [1], x = [0], y = [0])
# Creating the color bar and setting the
# color bar position at (5,6)
cb = ColorBar(color_mapper = color, location = (5,6))
# Adding the color bar to the right side
plot.add_layout(cb, 'right')
# Showing the above plot
show(plot)
Python3
# importing linear color mapper and colorbar
# from bokeh.models
from bokeh.models import LinearColorMapper, ColorBar
# importing transform from bokeh.transform
from bokeh.transform import transform
# importing figure and show from
# bokeh.plotting
from bokeh.plotting import figure,show
# importing pandas library
import pandas as pd
# Creating a set of data in d
d={'Col0':[ 190, 320, 270, 874, 459, 124, 546,
285, 341, 980, 1002, 453, 324, 245],
'Col1':[ 71, 128, 34, 49, 52, 87, 78, 25, 67,
19, 34, 100, 287, 55],
'Col2':[ 1123, 6471, 8345, 3253, 6420, 1830,
7849, 2937, 2108, 5392, 1273, 3928, 4927, 7392]}
# Converting the set of data into
# a dataframe
df = pd.DataFrame(d)
# Using linear color mapper, we are
# deciding the color of our color
# bar palette and also defining the
# lowest and highest values
color = LinearColorMapper(palette = 'Viridis256',
low = df.Col0.min(),
high = df.Col0.max())
# Creating a figure where we define
# its height and width along with its x-Axis
# label and Y-Axis Label
colorbar = figure(plot_width = 750, plot_height = 600,
x_axis_label = 'Col1', y_axis_label = 'Col2')
# Ploting the points in the graph using
# circles where color of the circles will be
# according to their values in the color bar
# along with defined size and opacity
colorbar.circle(x = 'Col1', y = 'Col2',
source = df, color = transform('Col0', color),
size = 15, alpha = 0.5)
# Defining various other features in the
# color bar such as its location in the
# plot along with its title
color_bar = ColorBar(color_mapper = color,
label_standoff = 14,
location = (0,0),
title = 'Plot')
# Defining the position of the color bar
colorbar.add_layout(color_bar, 'right')
# Showing the above implementation
show(colorbar)
Python3
# importing figure and show from bokeh.plotting
# module
from bokeh.plotting import figure,show
# importing autompg from bokeh.sampledata.autompg
from bokeh.sampledata.autompg import autompg
# importing LinearColorMapper and ColorBar
# from bokeh.models
from bokeh.models import LinearColorMapper, ColorBar
# importing transform from bokeh.transform
from bokeh.transform import transform
print(autompg)
# Defining the palette color along with
# the max and min values of the color bar
color_mapper = LinearColorMapper(palette = "Viridis256",
low = autompg.weight.min(),
high = autompg.weight.max())
# Creating an empty figure where we define the
# X-Axis label, Y-Axis Label
p = figure(x_axis_label = 'Horsepower',
y_axis_label = 'MPG')
# Drawing circular plots in the graph with
# color based on the value from the
# color-mapper
p.circle(x = 'hp', y = 'mpg', color = transform('weight',
color_mapper),
size = 20, alpha = 0.6, source = autompg)
# Defining properties of color mapper
color_bar = ColorBar(color_mapper = color_mapper,
label_standoff = 12,
location = (0,0),
title = 'Weight')
# Defining position of the color-mapper
p.add_layout(color_bar, 'right')
# Showing the plot
show(p)
输出:
代码说明:在代码中导入所有必要的包后,我们正在创建一个标量数据 d ,我们将在其中存储从 0 到 1 的值。之后,使用LinearColorMapper ,我们定义调色板的颜色和范围色条。然后,我们将创建一个图形,其中我们将 X 轴范围和 Y 轴范围设置为从 0 到 1。之后,我们使用 .image() 绘制上面的图,其中我们定义了图形的宽度和高度将绘图的起点作为参数进行绘图。因此,在固定颜色图的位置和标签后,我们最终将上述绘图显示为 HTML 文件。
示例 2:
除了创建热图,我们还可以创建一个简单的图并为其添加颜色条。但为此,我们需要在Python中创建一个数据框。如果我们正在使用它或者如果我们使用 google colab ,我们需要在我们的本地设备中安装 pandas ,那么我们不需要安装任何东西。打开命令提示符并编写以下代码:
pip install pandas
创建数据框后,我们可以处理数据并为其添加颜色条。以下实现如下所示:
在第二个示例中,我们没有创建热图。相反,我们正在创建一个图,其中点绘制在图形上的圆形形状中,颜色基于颜色条中的颜色。因此,为了实现这一点,我们需要创建一组需要进行数据框化的数据,以便以表格格式获取它们。完成上述实现后,我们将使用LinearColorMapper()来定义颜色图的颜色和范围。之后,我们将创建一个图形,在其中定义绘图宽度和绘图高度以及 X 和 Y 轴标签。然后,我们定义绘制的点的形状,即圆形,同时我们正在使用transform()在其中定义我们的颜色图以及每个点(x,y)将携带的值地点。这意味着在每一点颜色都将根据它携带的值而定,并且这些值由数据帧的“col0”组成。最后,我们展示了我们的结果图。
代码:
蟒蛇3
# importing linear color mapper and colorbar
# from bokeh.models
from bokeh.models import LinearColorMapper, ColorBar
# importing transform from bokeh.transform
from bokeh.transform import transform
# importing figure and show from
# bokeh.plotting
from bokeh.plotting import figure,show
# importing pandas library
import pandas as pd
# Creating a set of data in d
d={'Col0':[ 190, 320, 270, 874, 459, 124, 546,
285, 341, 980, 1002, 453, 324, 245],
'Col1':[ 71, 128, 34, 49, 52, 87, 78, 25, 67,
19, 34, 100, 287, 55],
'Col2':[ 1123, 6471, 8345, 3253, 6420, 1830,
7849, 2937, 2108, 5392, 1273, 3928, 4927, 7392]}
# Converting the set of data into
# a dataframe
df = pd.DataFrame(d)
# Using linear color mapper, we are
# deciding the color of our color
# bar palette and also defining the
# lowest and highest values
color = LinearColorMapper(palette = 'Viridis256',
low = df.Col0.min(),
high = df.Col0.max())
# Creating a figure where we define
# its height and width along with its x-Axis
# label and Y-Axis Label
colorbar = figure(plot_width = 750, plot_height = 600,
x_axis_label = 'Col1', y_axis_label = 'Col2')
# Ploting the points in the graph using
# circles where color of the circles will be
# according to their values in the color bar
# along with defined size and opacity
colorbar.circle(x = 'Col1', y = 'Col2',
source = df, color = transform('Col0', color),
size = 15, alpha = 0.5)
# Defining various other features in the
# color bar such as its location in the
# plot along with its title
color_bar = ColorBar(color_mapper = color,
label_standoff = 14,
location = (0,0),
title = 'Plot')
# Defining the position of the color bar
colorbar.add_layout(color_bar, 'right')
# Showing the above implementation
show(colorbar)
输出:
示例 3:
在此示例中,我们将从bokeh.sampledata.autompg导入数据集“ autompg ”。
蟒蛇3
# importing figure and show from bokeh.plotting
# module
from bokeh.plotting import figure,show
# importing autompg from bokeh.sampledata.autompg
from bokeh.sampledata.autompg import autompg
# importing LinearColorMapper and ColorBar
# from bokeh.models
from bokeh.models import LinearColorMapper, ColorBar
# importing transform from bokeh.transform
from bokeh.transform import transform
print(autompg)
# Defining the palette color along with
# the max and min values of the color bar
color_mapper = LinearColorMapper(palette = "Viridis256",
low = autompg.weight.min(),
high = autompg.weight.max())
# Creating an empty figure where we define the
# X-Axis label, Y-Axis Label
p = figure(x_axis_label = 'Horsepower',
y_axis_label = 'MPG')
# Drawing circular plots in the graph with
# color based on the value from the
# color-mapper
p.circle(x = 'hp', y = 'mpg', color = transform('weight',
color_mapper),
size = 20, alpha = 0.6, source = autompg)
# Defining properties of color mapper
color_bar = ColorBar(color_mapper = color_mapper,
label_standoff = 12,
location = (0,0),
title = 'Weight')
# Defining position of the color-mapper
p.add_layout(color_bar, 'right')
# Showing the plot
show(p)
输出:
在第三个示例中,我们使用 bokeh.sampledata.autompg 导入数据集,因为它已经是表格格式,我们不需要使用数据帧。然后我们遵循与示例 2相同的步骤并显示上图。