#-*- coding: utf-8 -*-
from bokeh.models import HoverTool
from bokeh.plotting import figure, show, output_file
import pandas as pd
#scikit-learn 库内置著名的Iris植物分类数据集
from sklearn.datasets import load_iris
# 源数据data读取
dataset = load_iris()
data = pd.DataFrame(dataset.data,columns = ('sepalLength','sepalWidth','petalLength','petalWidth'))
data['species'] = dataset.target
# 工具列表
TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,"
# 创建画图
p1 = figure(plot_width=900, plot_height=500, title="Iris Morphology",x_axis_label='Length',y_axis_label='Width',tools=TOOLS)
# 颜色列表
colors = ["red", "olive", "darkred", "goldenrod", "orange", "skyblue", "salmon"]
for i in range(0,3):
# 分类数据
species_sepalLength_x = list(data[data['species']==i]['sepalLength'])
species_sepalWidth_y = list(data[data['species']==i]['sepalWidth'])
species_petalLength_x = list(data[data['species']==i]['petalLength'])
species_petalWidth_y = list(data[data['species']==i]['petalWidth'])
# 这里,以颜色区分种类,以形状区分数据的不同
# 画点图
p1.circle(x=species_sepalLength_x, y=species_sepalWidth_y, color=colors[i],size=10,legend='sepal_species: %s'%i,fill_alpha=0.2)
# 画十字图
p1.cross(x=species_petalLength_x,y=species_petalWidth_y,color=colors[i],size=10,legend='petal_species: %s'%i,alpha=0.7)
# 图例的位置
p1.legend.location = "top_left"
# 图例的字体颜色
p1.legend.label_text_color = 'black'
# 图例的背景颜色
p1.legend.background_fill_color = "darkgrey"
# 自定义工具显示
hover = p1.select_one(HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = [
("(Length, Width)", "($x, $y)"),
]
# 保存在本地
output_file("Iris.html")
# 输出到显示器上
show(p1)
Iris植物分类数据可视化散点图(bokeh)
最新推荐文章于 2025-05-30 22:25:14 发布