鸢尾花的可视化(散点图、小提琴图、以及pyearchs平行坐标图)

本文主要是介绍鸢尾花的可视化(散点图、小提琴图、以及pyearchs平行坐标图),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、鸢尾花散点图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd# 数据准备
from sklearn import datasetsiris = datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class'])
print(pd_iris)
plt.figure(dpi=150)  # 设置图的分辨率# plt.style.use('Solarize_Light2')  # 使用Solarize_Light2风格绘图 橘色加白色网格线
# plt.style.use('seaborn-white')  #风格为全白
plt.style.use('seaborn')  #风格为浅蓝色加白色网格线iris_type = pd_iris['class'].unique()  # 根据class列将点分为三类,获得三种类型[0. 1. 2.]
print(iris_type)iris_name = iris.target_names  # 获取每一类的名称
# print(iris_name)#['setosa' 'versicolor' 'virginica']colors = ['#c72e29', '#098154', '#fb832d']  # 三种不同颜色
markers = ['$\clubsuit$', 'o', '^']  # 三种不同图形 #$\clubsuit$  ♣形状  '^'为三角形状for i in range(len(iris_type)):plt.scatter(pd_iris.loc[pd_iris['class'] == iris_type[i], 'sepal_length'],  # 传入数据xpd_iris.loc[pd_iris['class'] == iris_type[i], 'sepal_width'],  # 传入数据ys=50,  # 散点图形(marker)的大小c=colors[i],  # marker颜色marker=markers[i],  # marker形状# marker=matplotlib.markers.MarkerStyle(marker = markers[i],fillstyle='full'),#设置marker的填充alpha=0.8,  # marker透明度,范围为0-1facecolors='r',  # marker的填充颜色,当上面c参数设置了颜色,优先cedgecolors='none',  # marker的边缘线色linewidths=1,  # marker边缘线宽度,edgecolors不设置时,该参数不起作用label=iris_name[i])  # 后面图例的名称取自labelplt.legend(loc='upper right')
plt.show()

说明其中代码难点

#取出setosa组的子dataFrame 而且全是setosa类别
setosa=pd_iris[pd_iris['class'] == iris_type[0]]
# print(setosa)

#pd.loc[行,列],在行的选择中引入了条件选取
B=pd_iris.loc[pd_iris['class'] == iris_type[0], 'sepal_length'] 
# print(B)

散点图如下:

 二、鸢尾花的箱型图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd# 数据准备
from sklearn import datasetsiris = datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class'])
print(pd_iris)
plt.figure(dpi=150)  # 设置图的分辨率
plt.style.use('seaborn-white')  #风格为全白color_list=["#D1B6E1",'#30A9DE','#58C9B9','#9DC8C8']
lables=['sepal_length', 'sepal_width', 'petal_length', 'petal_width']f=plt.boxplot(pd_iris[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']],labels=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'],patch_artist=True,sym='o',#异常值的形状showbox=True,showmeans = True,widths=0.2)for box, c in zip(f['boxes'], color_list):  # 对箱线图设置颜色box.set(color=c, linewidth=2)box.set(facecolor=c)# 这里设置的是各个box的其他属性
for whisker in f['whiskers']: #设置箱型图的端点与箱子的连接线whisker.set(color='r', linewidth=0.5)
for cap in f['caps']:  #设置箱型图的端点线cap.set(color='g', linewidth=1)
for median in f['medians']:  #设置medians线median.set(color='DarkBlue', linewidth=1)
for flier in f['fliers']:flier.set(marker='o', color='y', alpha=0.5)
for means in f['means']:means.set(color='black')plt.title('my boxplot')
plt.show()

箱型图如下:

三、鸢尾花的小提琴图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns# 数据准备
from sklearn import datasetsiris = datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class'])
print(pd_iris)
plt.figure(dpi=150)  # 设置图的分辨率
plt.style.use('seaborn-white')  #风格为全白label = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
font_1 = {"size": 14}sns.violinplot(data =pd_iris[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']])
plt.xlabel("category", font_1)
plt.ylabel("Length or width", font_1)
plt.xticks(ticks = [0, 1, 2 ,3], labels = label, fontsize = 11)
plt.yticks(fontsize = 12)
plt.show()

小提琴图如下:

四、鸢尾花的平行坐标图

from pyecharts import options as opts
from pyecharts.charts import Parallel
import pandas as pd#读数据
result=pd.read_csv('iris.csv')
print(result)
#分组,按照花的类型分组
A=result[result['species']=='setosa']
B=result[result['species']=='versicolor']
C=result[result['species']=='virginica']
print(result['sepal_length'].max())
#Iris-setosa的数据
data1=[]
for a in range(0,len(A)):   # 计算列表A的长度,进行循环切片data1.append(A.values.tolist()[a][0:])
#Iris-versicolor的数据
data2=[]
for b in range(0,len(B)):data2.append(B.values.tolist()[b][0:])
#Iris-virginica的数据
data3=[]
for c in range(0,len(C)):data3.append(C.values.tolist()[c][0:])
#输出数据看数据的形式是否正确
print(data1,'\n',data2,'\n',data3)c = (     Parallel().add_schema([opts.ParallelAxisOpts(dim=0, name="sepal length",is_scale=True ,min_=4,max_=8),opts.ParallelAxisOpts(dim=1, name="sepal width",is_scale=True,min_=2,max_=4.5),opts.ParallelAxisOpts(dim=2, name="petal length",is_scale=True,min_=1,max_=7),opts.ParallelAxisOpts(dim=3, name="petal width",is_scale=True,min_=0,max_=2.5),#自动划分范围is_scale=Trueopts.ParallelAxisOpts(dim=4,  name="分类", type_="category",data=["setosa", "versicolor", "virginica"]),]).add("setosa", data1).add("versicolor", data2).add("virginica", data3).set_global_opts(title_opts=opts.TitleOpts(title="Parallel cordlnate plot, Fisher's iris data",pos_top="top",pos_right="center"),legend_opts=opts.LegendOpts( pos_top="bottom",pos_right="center"),yaxis_opts=opts.AxisOpts(min_='dataMin',max_="dataMax")).render("parallel.html")
)

平行坐标图如下:

这篇关于鸢尾花的可视化(散点图、小提琴图、以及pyearchs平行坐标图)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/405295

相关文章

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

基于Python打造一个可视化FTP服务器

《基于Python打造一个可视化FTP服务器》在日常办公和团队协作中,文件共享是一个不可或缺的需求,所以本文将使用Python+Tkinter+pyftpdlib开发一款可视化FTP服务器,有需要的小... 目录1. 概述2. 功能介绍3. 如何使用4. 代码解析5. 运行效果6.相关源码7. 总结与展望1

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1