Matplotlib散点图加legend

简介: Matplotlib散点图加legend

实例(Machine Learning in Action):


一般情况下,简单绘制散点图:


import matplotlib.pyplot as plt
dating_data_mat, datingLabel = TxtToNumpy.TxtToNumpy("datingTestSet2.txt")
fig = plt.figure(figsize = (10, 6))
ax = fig.add_subplot(111)
 #x轴、y轴、点大小、点颜色
ax.scatter(dating_data_mat[:, 0], dating_data_mat[:, 1], 15.0*array(datingLabel), 15.0*array(datingLabel))
plt.xlabel("Frequent Flyier Miles Earned Per Year")
plt.ylabel("Percentage of Time Spent Playing Video Games")
plt.show()


得到的图像:


0a2653c851af460fa595bd959398a8f1.png


加上legend,只需要将不同标签分类到几个数据集中,然后就可以按照分类来加legend了:


import matplotlib.pyplot as plt
dating_data_mat, datingLabel = TxtToNumpy.TxtToNumpy("datingTestSet2.txt")
type1_x = []; type1_y = []
type2_x = []; type2_y = []
type3_x = []; type3_y = []
for i in range(len(datingLabel)):
    if datingLabel[i] == 1: #第i行的label为1时
        type1_x.append(dating_data_mat[i][0])
        type1_y.append(dating_data_mat[i][1])
    if datingLabel[i] == 2: #第i行的label为2时
        type2_x.append(dating_data_mat[i][0])
        type2_y.append(dating_data_mat[i][1])
    if datingLabel[i] == 3: #第i行的label为3时
        type3_x.append(dating_data_mat[i][0])
        type3_y.append(dating_data_mat[i][1])
fig = plt.figure(figsize = (10, 6))
ax = fig.add_subplot(111)
type1 = ax.scatter(type1_x, type1_y, s = 30, c = 'brown')
type2 = ax.scatter(type2_x, type2_y, s = 30, c = 'lime')
type3 = ax.scatter(type3_x, type3_y, s = 30, c = "darkviolet")
plt.xlabel("Frequent Flyier Miles Earned Per Year")
plt.ylabel("Percentage of Time Spent Playing Video Games")
ax.legend((type1, type2, type3), ("DidntLike", "SmallDoses", "LargeDoses"), loc = 0)
plt.show()


得到的图像:


2d65d23f6d4748949b924e4057485923.png

相关文章
|
8月前
|
数据可视化 Python
【100天精通Python】Day62:Python可视化_Matplotlib绘图基础,绘制折线图、散点图、柱状图、直方图和饼图,以及自定义图标外观和功能,示例+代码
【100天精通Python】Day62:Python可视化_Matplotlib绘图基础,绘制折线图、散点图、柱状图、直方图和饼图,以及自定义图标外观和功能,示例+代码
135 0
|
2天前
|
Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-2
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
2天前
|
数据可视化 开发者 Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-1
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
1月前
|
Python
matplotlib-散点图
matplotlib-散点图
matplotlib-散点图
|
1月前
|
数据可视化 数据挖掘 Python
Matplotlib图表类型详解:折线图、柱状图与散点图
【4月更文挑战第17天】本文介绍了Python数据可视化库Matplotlib的三种主要图表类型:折线图、柱状图和散点图。折线图用于显示数据随时间或连续变量的变化趋势,适合多条曲线对比;柱状图适用于展示分类数据的数值大小和比较;散点图则用于揭示两个变量之间的关系和模式。通过示例代码展示了如何使用Matplotlib创建这些图表。
|
7月前
|
数据挖掘 Python
【Python】数据分析:matplotlib散点图
【Python】数据分析:matplotlib散点图
54 0
|
8月前
|
数据可视化 Python
【100天精通Python】Day65:Python可视化_Matplotlib3D绘图mplot3d,绘制3D散点图、3D线图和3D条形图,示例+代码
【100天精通Python】Day65:Python可视化_Matplotlib3D绘图mplot3d,绘制3D散点图、3D线图和3D条形图,示例+代码
219 0
|
数据可视化 Python
可视化库Matplotlib-条形图与散点图
可视化库Matplotlib-条形图与散点图
可视化库Matplotlib-条形图与散点图
|
开发者 Python
matplotlib画折线图、直方图、饼图、散点图等常见图形
matplotlib画折线图、直方图、饼图、散点图等常见图形
224 0
matplotlib画折线图、直方图、饼图、散点图等常见图形
|
3天前
|
数据可视化 Python Windows
使用Python进行数据可视化(一、matplotlib)
使用Python进行数据可视化(一、matplotlib)