Iris植物分类数据可视化散点图(bokeh)


#-*- 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)

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值