python scatter legend_Python可视化|matplotlib10-绘制散点图scatter
详细介绍鸢尾花iris数据集;
matplotlib.pyplot.scatter绘制散点图
matplotlib.axes.Axes.scatter绘制散点图
本文速览
目录1、鸢尾花(iris)数据集
数据集导入、查看特征
DESCR
data
feature_names
target
target_names
将鸢尾花数据集转为DataFrame数据集
2、matplotlib.pyplot.scatter法绘制散点图
3、matplotlib.axes.Axes.scatter法绘制散点图
1、鸢尾花(iris)数据集数据集导入、查看特征
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
from sklearn import datasets
iris=datasets.load_iris()
dir(iris)
['DESCR', 'data', 'feature_names', 'target', 'target_names']DESCR
#DESCR为数据集的描述信息,输出来看看:
print(iris.DESCR)Iris Plants Database
====================
Notes
-----
Data Set Characteristics:
:Number of Instances: 150 (50 in each of three classes)
:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:#四列数据的四个特征
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
- class:#数据描述三类鸢尾花
- Iris-Setosa
- Iris-Versicolour
- Iris-Virginica
:Summary Statistics:#四列数据的简单统计信息
============== ==== ==== ======= ===== ====================
Min Max Mean SD Class Correlation
============== ==== ==== ======= ===== ====================
sepal length: 4.3 7.9 5.84 0.83 0.7826
sepal width: 2.0 4.4 3.05 0.43 -0.4194
petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)
petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)
============== ==== ==== ======= ===== ====================
:Missing Attribute Values: None
:Class Distribution: 33.3% for each of 3 classes.
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
:Date: July, 1988
This is a copy of UCI ML iris datasets.
http://archive.ics.uci.edu/ml/datasets/Iris
The famous Iris database, first used by Sir R.A Fisher
This is perhaps the best known database to be found in the
pattern recognition literature. Fisher's paper is a classic in the field and
is referenced frequently to this day. (See Duda & Hart, for example.) The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant. One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.
References
----------
- Fisher,R.A. "The use of multiple measurements in taxonomic problems"
Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
Mathematical Statistics" (John Wiley, NY, 1950).
- Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.
(Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.
- Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
Structure and Classification Rule for Recognition in Partially Exposed
Environments". IEEE Transactions on Pattern Analysis and Machine
Intelligence, Vol. PAMI-2, No. 1, 67-71.
- Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions
on Information Theory, May 1972, 431-433.
- See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II
conceptual clustering system finds 3 classes in the data.
- Many, many more ...
data
鸢尾花四个特征的数据。
print(type(iris.data))
print(iris.data.shape)
iris.data[:10,:]
#数据格式为numpy.ndarray
(150, 4)#数据集大小为150行4列
array([[5.1, 3.5, 1.4, 0.2],#数据集前十行
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5. , 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.1]])feature_names
以上4列数据的名称,从左到右依次为花萼长度、花萼宽度、花瓣长度、花瓣宽度,单位都是cm。
print(iris.feature_names)
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']target
使用数字0. ,1. ,2.标识每行数据代表什么类的鸢尾花。
print(iris.target)#150个元素的list
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]target_names
鸢尾花的名称,Setosa(山鸢尾花)、Versicolour(杂色鸢尾花)、Virginica(维吉尼亚鸢尾花)。
print(iris.target_names)
['setosa' 'versicolor' 'virginica']将鸢尾花数据集转为DataFrame数据集
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class'] )
#np.hstack()类似linux中的paste
#np.vstack()类似linux中的cat
pd_iris.head()
2、matplotlib.pyplot.scatter法绘制散点图取数据集前两列绘制简单散点图
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
#数据准备
from sklearn import datasets
iris=datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class'] )
plt.figure(dpi=100)
plt.scatter(pd_iris['sepal length(cm)'],pd_iris['sepal width(cm)'])
#根据sepal length(cm)和sepal width(cm)两列,每一行两个数值确定的点绘制到figure上即为散点三种不同鸢尾花的数据使用不同的图形(marker)和颜色表示
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
#数据准备
from sklearn import datasets
iris=datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class'] )
plt.figure(dpi=150)#设置图的分辨率
plt.style.use('Solarize_Light2')#使用Solarize_Light2风格绘图
iris_type=pd_iris['class'].unique()#根据class列将点分为三类
iris_name=iris.target_names#获取每一类的名称
colors = ['#c72e29','#098154','#fb832d']#三种不同颜色
markers = ['$\clubsuit$','.','+']#三种不同图形
for i in range(len(iris_type)):
plt.scatter(pd_iris.loc[pd_iris['class'] == iris_type[i], 'sepal length(cm)'],#传入数据x
pd_iris.loc[pd_iris['class'] == iris_type[i], 'sepal width(cm)'],#传入数据y
s = 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-1
facecolors='r',#marker的填充颜色,当上面c参数设置了颜色,优先c
edgecolors='none',#marker的边缘线色
linewidths=1,#marker边缘线宽度,edgecolors不设置时,该参数不起作用
label = iris_name[i])#后面图例的名称取自label
plt.legend(loc = 'upper right')
3、matplotlib.axes.Axes.scatter法绘制散点图
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
#数据准备
from sklearn import datasets
iris=datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class'] )
fig,ax = plt.subplots(dpi=150)
iris_type=pd_iris['class'].unique()#根据class列将点分为三类
iris_name=iris.target_names#获取每一类的名称
colors = ['#c72e29','#098154','#fb832d']#三种不同颜色
markers = ['$\clubsuit$','.','+']#三种不同图形
for i in range(len(iris_type)):
plt.scatter(pd_iris.loc[pd_iris['class'] == iris_type[i], 'sepal length(cm)'],#传入数据x
pd_iris.loc[pd_iris['class'] == iris_type[i], 'sepal width(cm)'],#传入数据y
s = 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-1
facecolors='r',#marker的填充颜色,当上面c参数设置了颜色,优先c
edgecolors='none',#marker的边缘线色
linewidths=1,#marker边缘线宽度,edgecolors不设置时,改参数不起作用
label = iris_name[i])#后面图例的名称取自label
plt.legend(loc = 'upper right')
4、参考资料
python scatter legend_Python可视化|matplotlib10-绘制散点图scatter相关推荐
- python使用matplotlib可视化subplots绘制子图、自定义几行几列子图,如果M行N列,那么最终包含M*N个子图、在指定的子图中添加可视化结果
python使用matplotlib可视化subplots绘制子图.自定义几行几列子图,如果M行N列,那么最终包含M*N个子图.在指定的子图中添加可视化结果 目录
- Python数据可视化——Plotly绘制散点图、堆积柱状图、饼图、旭日图、分布图、箱线图、时间序列图、多子图、k线图
文章目录 一.数据来源 二.导入数据 三.散点图Scatter 四.堆积柱状图Stacked Bar 五.饼图 六.旭日图Sunburst 七.分布图Distplot 八.箱线图Boxplot 九.热 ...
- python散点图拟合曲线-Python解决最小二乘法拟合并绘制散点图
问题背景 最近物理老师让用Excel弄一个最小二乘法拟合然后弄出方程来求玻尔兹曼常数.无奈发现Linux上的WPS没有绘图功能无语啊O__O"-,据说绘图功能是用delphi写的,不好做跨平 ...
- 【Matplotlib】【Python】如何使用matplotlib绘制散点图
目录 1.绘制单个点 2.scatter()绘制一系列点 3.修改或删除轮廓颜色 4.修改点颜色 plot函数可以实现绘制折线图,scatter()函数可以绘制散点图. 1.绘制单个点 使用方法:向s ...
- 用python画xy散点图-python使用Plotly绘图工具绘制散点图、线形图
今天在研究Plotly绘制散点图的方法,供大家参考,具体内容如下 使用Python3.6 + Plotly Plotly版本2.0.0 在开始之前先说说,还需要安装库Numpy,安装方法在我的另一篇博 ...
- Matplotlib可视化②——3D绘制散点图曲面图折线图等高线图
公众号: 数据小斑马,关注即可获得价值1000元的数据分析学习资料 数据可视化系列汇总: Matplotlib可视化①--二维图表绘制(折线图&直方图&散点图&条形图& ...
- python画熊猫代码_Python 绘制散点图(Pandas + Matplotlib)
简单绘制一个散点图. 数据使用小朋友的身高和体重,简单看看. 数据结构:下面看一下其中几条的部分内容,我们只需要其中身高(Height)和体重(Weight)列的数据: 共七万五千多条,下面看看通过p ...
- 使用python进行相关性分析并绘制散点图
近期,有小伙伴问我关于怎么使用python进行散点图的绘制,这个东西很简单,但是怎么讲相关性的值标注在图形上略显麻烦,因此,在这里记录一下,将整个流程展示一下. 需要用到的库 在本篇博客中,主要用到的 ...
- Python matplotlib数据可视化 subplot绘制多个子图
数据可视化的时候,有时需要将多个子图放在同一个画板上进行比较.通过使用GridSpec类配合subplot,可以很容易对子区域进行划定和选择,在同一个画板上绘制多个子图. 原文链接:https://y ...
最新文章
- 计算机一直在启动修复怎么关机,电脑开机一直要启动修复,自动修复好久开不了机,然后进去系统恢复选?...
- Myeclipse优化设置,加速你的开发武器
- 从Linux上查看接口路由IP(吓一跳ip)
- POJ 1228 —— “稳定”凸包
- android 图片放大缩小_贴在手机上的显微镜,轻松放大400倍,化身“蚁人”玩转微观世界...
- Git:切换分支时,无法切换到分支
- vue点击网页全屏_vue中实现点击变成全屏的多种方法
- zynq开发系列2:GPIO连接MIO控制LED闪烁(SDK端代码编写详解)
- Android中Adapter之BaseAdapter使用
- 如何批量删除.svn文件
- 很棒的VC界面库 - GuiToolkit
- [和秋叶一起学ppt]四步教你变身高富帅ppt(笔记)
- linux alsa 不创建声卡能否创建pcm设备,Linux ALSA声卡驱动之一:声卡的创建
- NetBeans配置subli
- CDISC STANDARD
- 文件拷贝(6种方式)
- Cocoa-专业术语
- Android 实现水波纹效果
- Fxfactory插件:复古电影调色插件Sheffield Softworks Vintage
- Word论文引用和目录生成方法