使用matplotlib中scatter方法画散点图
作者:bitcarmanlee 发布时间:2022-10-16 02:02:39
本文实例为大家分享了用matplotlib中scatter方法画散点图的具体代码,供大家参考,具体内容如下
1、最简单的绘制方式
绘制散点图是数据分析过程中的常见需求。python中最有名的画图工具是matplotlib,matplotlib中的scatter方法可以方便实现画散点图的需求。下面我们来绘制一个最简单的散点图。
数据格式如下:
0 746403
1 1263043
2 982360
3 1202602
...
其中第一列为X坐标,第二列为Y坐标。下面我们来画图。
#!/usr/bin/env python
#coding:utf-8
import matplotlib.pyplot as plt
def pltpicture():
file = "xxx"
xlist = []
ylist = []
with open(file, "r") as f:
for line in f.readlines():
lines = line.strip().split()
if len(lines) != 2 or int(lines[1]) < 100000:
continue
x, y = int(lines[0]), int(lines[1])
xlist.append(x)
ylist.append(y)
plt.xlabel('X')
plt.ylabel('Y')
plt.scatter(xlist, ylist)
plt.show()
2、更漂亮一些的画图方式
上面的图片比较粗糙,是最简单的方式,没有任何相关的配置项。下面我们再用另外一份数据集画出更漂亮一点的图。
数据集来自网络的公开数据集,数据格式如下:
40920 8.326976 0.953952 3
14488 7.153469 1.673904 2
26052 1.441871 0.805124 1
75136 13.147394 0.428964 1
...
第一列每年获得的飞行常客里程数;
第二列玩视频游戏所耗时间百分比;
第三列每周消费的冰淇淋公升数;
第四列为label:
1表示不喜欢的人
2表示魅力一般的人
3表示极具魅力的人
现在将每年获取的飞行里程数作为X坐标,玩视频游戏所消耗的事件百分比作为Y坐标,画出图。
from matplotlib import pyplot as plt
file = "/home/mi/wanglei/data/datingTestSet2.txt"
label1X, label1Y, label2X, label2Y, label3X, label3Y = [], [], [], [], [], []
with open(file, "r") as f:
for line in f:
lines = line.strip().split()
if len(lines) != 4:
continue
distance, rate, label = lines[0], lines[1], lines[3]
if label == "1":
label1X.append(distance)
label1Y.append(rate)
elif label == "2":
label2X.append(distance)
label2Y.append(rate)
elif label == "3":
label3X.append(distance)
label3Y.append(rate)
plt.figure(figsize=(8, 5), dpi=80)
axes = plt.subplot(111)
label1 = axes.scatter(label1X, label1Y, s=20, c="red")
label2 = axes.scatter(label2X, label2Y, s=40, c="green")
label3 = axes.scatter(label3X, label3Y, s=50, c="blue")
plt.xlabel("every year fly distance")
plt.ylabel("play video game rate")
axes.legend((label1, label2, label3), ("don't like", "attraction common", "attraction perfect"), loc=2)
plt.show()
最后效果图:
3、scatter函数详解
我们来看看scatter函数的签名:
def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
vmin=None, vmax=None, alpha=None, linewidths=None,
verts=None, edgecolors=None,
**kwargs):
"""
Make a scatter plot of `x` vs `y`
Marker size is scaled by `s` and marker color is mapped to `c`
Parameters
----------
x, y : array_like, shape (n, )
Input data
s : scalar or array_like, shape (n, ), optional
size in points^2. Default is `rcParams['lines.markersize'] ** 2`.
c : color, sequence, or sequence of color, optional, default: 'b'
`c` can be a single color format string, or a sequence of color
specifications of length `N`, or a sequence of `N` numbers to be
mapped to colors using the `cmap` and `norm` specified via kwargs
(see below). Note that `c` should not be a single numeric RGB or
RGBA sequence because that is indistinguishable from an array of
values to be colormapped. `c` can be a 2-D array in which the
rows are RGB or RGBA, however, including the case of a single
row to specify the same color for all points.
marker : `~matplotlib.markers.MarkerStyle`, optional, default: 'o'
See `~matplotlib.markers` for more information on the different
styles of markers scatter supports. `marker` can be either
an instance of the class or the text shorthand for a particular
marker.
cmap : `~matplotlib.colors.Colormap`, optional, default: None
A `~matplotlib.colors.Colormap` instance or registered name.
`cmap` is only used if `c` is an array of floats. If None,
defaults to rc `image.cmap`.
norm : `~matplotlib.colors.Normalize`, optional, default: None
A `~matplotlib.colors.Normalize` instance is used to scale
luminance data to 0, 1. `norm` is only used if `c` is an array of
floats. If `None`, use the default :func:`normalize`.
vmin, vmax : scalar, optional, default: None
`vmin` and `vmax` are used in conjunction with `norm` to normalize
luminance data. If either are `None`, the min and max of the
color array is used. Note if you pass a `norm` instance, your
settings for `vmin` and `vmax` will be ignored.
alpha : scalar, optional, default: None
The alpha blending value, between 0 (transparent) and 1 (opaque)
linewidths : scalar or array_like, optional, default: None
If None, defaults to (lines.linewidth,).
verts : sequence of (x, y), optional
If `marker` is None, these vertices will be used to
construct the marker. The center of the marker is located
at (0,0) in normalized units. The overall marker is rescaled
by ``s``.
edgecolors : color or sequence of color, optional, default: None
If None, defaults to 'face'
If 'face', the edge color will always be the same as
the face color.
If it is 'none', the patch boundary will not
be drawn.
For non-filled markers, the `edgecolors` kwarg
is ignored and forced to 'face' internally.
Returns
-------
paths : `~matplotlib.collections.PathCollection`
Other parameters
----------------
kwargs : `~matplotlib.collections.Collection` properties
See Also
--------
plot : to plot scatter plots when markers are identical in size and
color
Notes
-----
* The `plot` function will be faster for scatterplots where markers
don't vary in size or color.
* Any or all of `x`, `y`, `s`, and `c` may be masked arrays, in which
case all masks will be combined and only unmasked points will be
plotted.
Fundamentally, scatter works with 1-D arrays; `x`, `y`, `s`, and `c`
may be input as 2-D arrays, but within scatter they will be
flattened. The exception is `c`, which will be flattened only if its
size matches the size of `x` and `y`.
Examples
--------
.. plot:: mpl_examples/shapes_and_collections/scatter_demo.py
"""
其中具体的参数含义如下:
x,y是相同长度的数组。
s可以是标量,或者与x,y长度相同的数组,表明散点的大小。默认为20。
c即color,表示点的颜色。
marker 是散点的形状。
来源:https://blog.csdn.net/bitcarmanlee/article/details/82707335
猜你喜欢
- 1.分析 我们在用 php 制作网站时,分类是很重要的,在分类下面又再分类这第二个分类称为次分类,而现在大多
- 我使用的是anaconda。我推荐大家使用anaconda,对环境依赖关系处理的比较好。不用浪费太多时间在安装模块上。首先安装pyinsta
- 在开发高并发系统时有三把利器用来保护系统:缓存、降级和限流!为了保证在业务高峰期,线上系统也能保证一定的弹性和稳定性,最有效的方案就是进行服
- 操作实现函数: 代码如下:Function AddWater(n) Set Photo = 
- 阅读上一篇:请给PNG8一个机会 系列二:对png8的误解Png8的在ie中的怪异表现:1.半透明的png8在ie6以下的浏览器显示为全透明
- 前言最近发现有些东西长时间不用就要忘了,坚持每天复习总结一个小知识点吧~异常是什么呢?就是在代码执行过程中非预期的执行结果,随着代码越来越复
- type指示type要使用的验证器。可识别的类型值为:string:类型必须为string。type 默认是 string// 校验stri
- 零基础学习Python的入手方向:1、首先你确定学习Python用来做什么方向,爬虫还是……;2、确定方向后,就按照你喜欢的方式找学习资料;
- 简介使用faker可以获取很多模拟数据,如:姓名、电话、地址、银行、汽车、条形码、公司、信用卡、email、user_agen等等学会使用这
- 协程的定义协程(Coroutine),又称微线程,纤程。(协程是一种用户态的轻量级线程)作用:在执行 A 函数的时候,可以随时中断,去执行
- 前言在安装完python及pip,setuptools等工具后,即可以创建virualenv虚拟环境了,这个类似于虚拟机的工具,可以让同一台
- 当使用Python的flask框架来开发网站后台,解析前端Post来的数据,通常都会使用request.form来获取前端传过来的数据,但是
- 将 PDF 转换为 CSV在机器学习中,我们应该少一些“数据清理”,多一些“数据
- 网页中使用flash可以增强页面的动态交互效果,特别是用flash来制作广告,效果更好。经常使用flash的人,可能就碰到了flash会遮住
- Knockout是一个以数据模型(data model)为基础的能够帮助你创建富文本,响应显示和编辑用户界面的JavaScript类库。任何
- 1.指定GPU运算如果安装的是GPU版本,在运行的过程中TensorFlow能够自动检测。如果检测到GPU,TensorFlow会尽可能的利
- 我们能否使用一些自动化工具代替人来完成数据分析的过程呢,现有一些成熟的 AutoEDA 工具可以一定程度上完成上述过程。本文中,我将盘点常见
- 本文实例讲述了Python实现求两个数组交集的方法。分享给大家供大家参考,具体如下:一、题目给定两个数组,编写一个函数来计算它们的交集。例1
- 为什么需要线程锁当我们访问一些特殊的数据时,需要保证该数据的原子性,比如: 文章的阅读量、文章的点赞量等。我们必须要确保这些共享数据必须是原
- 在利用javascript内置的eval函数,将json格式的字符串转换成JS对象时,需要用一对"()"先将该字符串包住