python Matplotlib 系列教程(四)——散点图

这一章节,我们将介绍散点图的绘制:

散点图经常用来显示分布或者比较几个变量的相关性或者分组。

要绘制单个点我们需要使用scatter()函数,先看一个最简单的示例:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)  

plt.title(u'散点图示例', FontProperties=font)
plt.scatter(2, 4)
plt.show()

这里写图片描述

实际运用中,我们可能希望以一种颜色进行显示较小的值,用另外一种颜色显示较大的值,绘制某些大型数据的时候,你还可以对每个点都设置相同的样式,然后对某些特定的点使用不同的样式来突出他们。

现在我们看一个稍微复杂一点的例子:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)  

xValue = list(range(0, 101))
yValue = [x * np.random.rand() for x in xValue]

plt.title(u'散点图示例', FontProperties=font)

plt.xlabel('x-value')
plt.ylabel('y-label')
# plt.scatter(x, y, s, c, marker)
# x: x轴坐标
# y:y轴坐标
# s:点的大小/粗细 标量或array_like 默认是 rcParams['lines.markersize'] ** 2
# c: 点的颜色 
# marker: 标记的样式 默认是 'o'
plt.legend()

plt.scatter(xValue, yValue, s=20, c="#ff1212", marker='o')
plt.show()

这里写图片描述

附录:marker的类型

markertype含义
“.”point
“,”pixel像素
“o”circle
“v”triangle_down下三角
“^”triangle_up上三角
“<”triangle_left左三角
“>”triangle_right右三角
“1”tri_down类似奔驰的标志
“2”tri_up类似奔驰的标志
“3”tri_left类似奔驰的标志
“4”tri_right类似奔驰的标志
“8”octagon八角形
“s”square正方形
“p”pentagon五角星
“*”star星号
“h”hexagon1六边形1
“H”hexagon2六边形2
“+”plus加号
“x”xx
“D”diamond钻石
“d”thin_diamond细的钻石
vline
“-“hline水平方向的线
“TICKLEFT”octagon像素
  • 29
    点赞
  • 161
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值