前言

散点图是Matplotlib常用图形之一,与线形图类似。但是这种图形不再由线段连接,而是由独立的点、圆圈或其他形状构成。那么怎么画散点图呢?Matplotlib给出了两种不同的方法,去画散点图。如何在不同的情况下,合理的使用这两种方法?

用plt.plot画散点图

"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import numpy as npimport matplotlib.pyplot as pltx= np.linspace(0, 10, 30)y= np.sin(x)plt.plot(x, y, 'o', color='black');pre>

plt.plot()函数的第三个参数是一个字符,表示图形符号的类型。与你之前用 '-' 和 '--' 设置线条属性类似,对应的图形标记也有缩写形式。

1.部分图形标记展示

"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">rng= np.random.RandomState(0)formarkerin['o', '.', ',', 'x', '+', 'v', '^', ', '>', 's', 'd']:plt.plot(rng.rand(5), rng.rand(5), marker,label="marker='{0}'".format(marker))plt.legend(numpoints=1)plt.xlim(0, 1.8);


2.连接每一个点

"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">plt.plot(x, y, '-ok')# 直线(-)、圆圈(o)、黑色(k)


用图形标记的缩写形式,跟线段组合成一给新的字符,传给plt.plot()函数

3.自定义线条和散点属性

"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">plt.plot(x, y, '-p', color='gray',markersize=15, linewidth=4,markerfacecolor='white',markeredgecolor='gray',markeredgewidth=2)plt.ylim(-1.2, 1.2)


plt.plot函数非常灵活,可以满足各种不同的可视化配置需求。

用plt.scatter画散点图

这是另一个创建散点图的函数是plt.scatter。它的功能非常强大,其用法与plt.plot函数类似。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">plt.scatter(x, y, marker='o')pre>

plot与scatter的区别

plt.scatter与plt.plot的主要差别在于,前者在创建散点图时具有更高的灵活性,可以 单独控制每个散点与数据匹配,也可以让每个散点具有不同的属性(大小、表面颜色、边 框颜色等)。

1.随机散点图

创建一个随机散点图,里面有各种颜色和大小的散点。为了能更好地显示重叠部分,用alpha参数来调整透明度。

"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">rng= np.random.RandomState(0)x= rng.randn(100)y= rng.randn(100)colors= rng.rand(100)sizes= 1000*rng.rand(100)plt.scatter(x, y, c=colors, s=sizes, alpha=0.3,cmap='viridis')plt.colorbar(); # 显示颜色条


注意点,颜色自动映射成颜色条(color scale,通过colorbar()显示),散点的大小以像素为单位。这样,散点的颜色与大小就可以在可视化图中显示多维数据的信息了。

plot与scatter:效率对比

plt.plot 与 plt.scatter 除了特征上的差异之外,还有什么影响我们选择的因素呢?在数 据量较小的时候,两者在效率上的差异不大。但是当数据变大到几千个散点时,plt.plot 的效率将大大高于 plt.scatter。

这是由于 plt.scatter 会对每个散点进行单独的大小与颜 色的渲染,因此渲染器会消耗更多的资源。而在 plt.plot 中,散点基本都彼此复制,因此整个数据集中所有点的颜色、尺寸只需要配置一次。

由于这两种方法在处理大型数据集时有很大的性能差异,因此面对大型数据集时,plt.plot 方法比 plt.scatter 方法好。

*声明:本文于网络整理,版权归原作者所有,如来源信息有误或侵犯权益,请联系我们删除或授权事宜。

觉得不错,点个“在看”然后转发出去

带圆圈大小的散点图_Python数据可视化,Matplotlib绘制“散点图”的两种方法!...相关推荐

  1. Mysql使用binlog恢复数据解决误操作问题的两种方法

    Mysql使用binlog恢复数据解决误操作问题的两种方法 参考文章: (1)Mysql使用binlog恢复数据解决误操作问题的两种方法 (2)https://www.cnblogs.com/Data ...

  2. c++ 显示三维散点图_Python数据可视化,Matplotlib绘制“散点图”的两种方法!

    前言 散点图是Matplotlib常用图形之一,与线形图类似.但是这种图形不再由线段连接,而是由独立的点.圆圈或其他形状构成.那么怎么画散点图呢?Matplotlib给出了两种不同的方法,去画散点图. ...

  3. 数据可视化: matplotlib绘制动态图及3维动画

    动画可以有趣地展示某种现象.相比于静态图表,人们更容易被动画和交互式的图表所吸引.在描绘时间序列数据时,动画更有意义,例如多年来股票价格的波动,过去十年气候的季节性变化和和趋势,因为我们可以看到特定参 ...

  4. Tableau可视化技巧-在数据上添加方向图标的两种方法

    在日常数据分析中,数据的上下波动是我们经常遇到的,比如指标的上升与下降.用一个方向图标,比如 ⬆️ 或者⬇️来表明这种变化有时候比数字更加容易辨别. 本文我们看一下如何在数据上添加方向图标. 如下图 ...

  5. python读取excel画散点图-python学习之matplotlib绘制散点图实例

    要绘制单个点,可使用函数scatter(),并向其传递一对x和y坐标,它将在指定位置绘制一个点: """使用scatter()绘制散点图""" ...

  6. python画散点图-python学习之matplotlib绘制散点图实例

    要绘制单个点,可使用函数scatter(),并向其传递一对x和y坐标,它将在指定位置绘制一个点: """使用scatter()绘制散点图""" ...

  7. 怎么把html数据导入excel,将网页表格数据导入到Excel中的两种方法

    将网页表格数据导入到Excel中的第一种方法: 第一步,将包括所需表格的网页打开,并按CTRL+C把网址复制到剪贴板,以备下一步使用. 第二步,打开运行Excel软件,单击菜单栏中的"数据→ ...

  8. python界面散点图_Python数据可视化——散点图

    PS: 翻了翻草稿箱, 发现居然存了一篇去年2月的文章...虽然naive,还是发出来吧... 本文记录了python中的数据可视化--散点图scatter, 令x作为数据(50个点,每个30维),我 ...

  9. python数据可视化散点图案例_Python数据可视化—散点图_python 数据可视化

    Python数据可视化-散点图 PS: 翻了翻草稿箱, 发现居然存了一篇去年2月的文章...虽然naive,还是发出来吧... 本文记录了Python中的数据可视化--散点图scatter, 令x作为 ...

最新文章

  1. 监听Settings的值的变化
  2. (转)mysql帮助命令使用说明
  3. vue-cli工程目录结构及相关文件说明
  4. 窗口背景颜色修改 备忘
  5. listView 多布局
  6. 提高篇 第三部分 图论 第1章 最小生成树
  7. android 数据回传代码,安卓向.net core api传输图片,执行保存到数据库命令后返回400错误代码,用postman测试没有问题安卓程序不行...
  8. JSPServlet精华笔记
  9. 如何在gradle上仅运行一个测试类
  10. Axios中无法运行 json-server【已解决】
  11. JavaSpring框架有哪些优势?
  12. Windows核心编程_HOOk SOCKET实现封包拦截
  13. 使用react-color实现前端取色器
  14. Visio绘图工具,“连接线”命令使用方法
  15. Hystrix解决雪崩问题
  16. 【JAVA】Retrofit详解和使用
  17. 为什么要认真准备Java面试,编程语言排行榜告诉你
  18. NLP - Gensim
  19. 金刚菩提子开裂自动修复此计算机,教你一招修复开裂的金刚菩提
  20. AI测试】人工智能测试整体介绍——第五部分

热门文章

  1. python程序详细描述_如何逐行描述Python代码?
  2. 漫画版:如何学习单片机?
  3. ubuntu18安装DC2016
  4. 十大垃圾专业 计算机,十大最好专业和最烂专业是什么
  5. 按照姓名升序排序的代码_干货:6种EXCEL排序方法,让老板对你刮目相看
  6. 牛客练习赛26 D xor序列 (线性基)
  7. POJ 2750 鸡兔同笼
  8. python3安装后无法使用退格键的问题
  9. 使用spring initialization创建SpringBoot项目
  10. keepalived+nginx实现高可用