侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

使用matplotlib中scatter方法画散点图

技术  /  管理员 发布于 7年前   177

本文实例为大家分享了用matplotlib中scatter方法画散点图的具体代码,供大家参考,具体内容如下

1、最简单的绘制方式

绘制散点图是数据分析过程中的常见需求。python中最有名的画图工具是matplotlib,matplotlib中的scatter方法可以方便实现画散点图的需求。下面我们来绘制一个最简单的散点图。

数据格式如下:

0   746403
1   1263043
2   982360
3   1202602
...

其中第一列为X坐标,第二列为Y坐标。下面我们来画图。

#!/usr/bin/env python#coding:utf-8import 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 pltfile = "/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 是散点的形状。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片
    下一条:
    matplotlib实现区域颜色填充
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客