热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python画图之散点图(plt.scatter)

散点图的应用很广泛,以前介绍过很多画图方法:Python画图(直方图、多张子图、二维图形、三维图形以及图中图),

        散点图的应用很广泛,以前介绍过很多画图方法:Python画图(直方图、多张子图、二维图形、三维图形以及图中图),漏掉了这个,现在补上,用法很简单,我们可以help(plt.scatter)看下它的用法:

Help on function scatter in module matplotlib.pyplot:scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None,
vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=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 datas : scalar or array_like, shape (n, ), optionalsize 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 eitheran instance of the class or the text shorthand for a particular marker.cmap : `~matplotlib.colors.Colormap`, optional, default: NoneA `~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: NoneA `~matplotlib.colors.Normalize` instance is used to scaleluminance 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: NoneThe alpha blending value, between 0 (transparent) and 1 (opaque) linewidths : scalar or array_like, optional, default: NoneIf None, defaults to (lines.linewidth,).verts : sequence of (x, y), optionalIf `marker` is None, these vertices will be used toconstruct the marker. The center of the marker is locatedat (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 asthe face color.If it is 'none', the patch boundary will notbe drawn.For non-filled markers, the `edgecolors` kwargis ignored and forced to 'face' internally.Returns-------paths : `~matplotlib.collections.PathCollection`Other parameters----------------kwargs : `~matplotlib.collections.Collection` propertiesSee Also--------plot : to plot scatter plots when markers are identical in size and colorNotes-----* 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 beflattened. The exception is `c`, which will be flattened only if its size matches the size of `x` and `y`.

我们可以看到参数比较多,平时主要用到的就是大小、颜色、样式这三个参数


s:形状的大小,默认 20,也可以是个数组,数组每个参数为对应点的大小,数值越大对应的图中的点越大。
c:形状的颜色,"b":blue   "g":green    "r":red   "c":cyan(蓝绿色,青色)  "m":magenta(洋红色,品红色) "y":yellow "k":black  "w":white
marker:常见的形状有如下
".":点                   ",":像素点           "o":圆形
"v":朝下三角形   "^":朝上三角形   "<":朝左三角形   ">":朝右三角形
"s":正方形           "p":五边星          "*":星型
"h":1号六角形     "H":2号六角形 

"+":+号标记      "x":x号标记
"D":菱形              "d":小型菱形 
"|":垂直线形         "_":水平线形


我们来看几个示例(在一张图显示了)

import matplotlib.pyplot as plt
import numpy as np
import pandas as pdx=np.array([3,5])
y=np.array([7,8])x1=np.random.randint(10,size=(25,))
y1=np.random.randint(10,size=(25,))plt.scatter(x,y,c=&#39;r&#39;)
plt.scatter(x1,y1,s=100,c=&#39;b&#39;,marker=&#39;*&#39;)#使用pandas来读取
x2=[]
y2=[]
rdata=pd.read_table(&#39;1.txt&#39;,header=None)
for i in range(len(rdata[0])):x2.append(rdata[0][i].split(&#39;,&#39;)[0])y2.append(rdata[0][i].split(&#39;,&#39;)[1])plt.scatter(x2,y2,s=200,c=&#39;g&#39;,marker=&#39;o&#39;)
plt.show()

 其中文档1.txt内容如下(上面图中的4个绿色大点)


5,6
7,9
3,4
2,7



推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • WhenIusepythontoapplythepymysqlmoduletoaddafieldtoatableinthemysqldatabase,itdo ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
  • [转载]从零开始学习OpenGL ES之四 – 光效
    继续我们的iPhoneOpenGLES之旅,我们将讨论光效。目前,我们没有加入任何光效。幸运的是,OpenGL在没有设置光效的情况下仍然可 ... [详细]
  • 浅解XXE与Portswigger Web Sec
    XXE与PortswiggerWebSec​相关链接:​博客园​安全脉搏​FreeBuf​XML的全称为XML外部实体注入,在学习的过程中发现有回显的XXE并不多,而 ... [详细]
  • 详解 Python 的二元算术运算,为什么说减法只是语法糖?[Python常见问题]
    原题|UnravellingbinaryarithmeticoperationsinPython作者|BrettCannon译者|豌豆花下猫(“Python猫 ... [详细]
  • 1.webkit内核中的一些私有的meta标签,这些meta标签在开发webapp时起到非常重要的作用(1) ... [详细]
  • 震惊,正儿八经的网页居然在手机上这样显示!
    本篇文章所描述的,是网页移动端开发中的一些概念,以及一些常用标签~一、像素基本知识设备物理像素:设备上的一个像素点设备无关像素࿱ ... [详细]
author-avatar
尘世聚散
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有