matplotlib scatter 绘制散点图

举报
千江有水千江月 发表于 2020/12/27 22:11:03 2020/12/27
【摘要】 所属的课程名称及链接[AI基础课程--常用框架工具]环境信息* ModelArts  * Notebook - Multi-Engine 2.0 (python3)    * JupyterLab - Notebook - Conda-python3      * matplotlib 2.1.0matplotlib scatter 绘制散点图import numpy as npimport...

所属的课程名称及链接


环境信息

  • * ModelArts
    •   * Notebook - Multi-Engine 2.0 (python3)
      •     * JupyterLab - Notebook - Conda-python3
        •       * matplotlib 2.1.0


matplotlib scatter 绘制散点图

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(0,20,15)
y = np.random.randint(0,20,15)

print("x",x)
print("y",y)

y_ticks = np.arange(0,21)
print("y_ticks",y_ticks)

plt.yticks(np.arange(0,21))

plt.scatter(x,y)
plt.show()
x [19  5 16 17  0  2  9 12  5 19  8  9  1 16  7]
y [16  9  6 17  5  4 10  6  4 14  5 10  3  2  7]
y_ticks [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

plt-scatter.png


help

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 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`.
    
    .. note::
        In addition to the above described arguments, this function can take a
        **data** keyword argument. If such a **data** argument is given, the
        following arguments are replaced by **data[<arg>]**:
    
        * All arguments with the following names: 'c', 'color', 'edgecolors', 'facecolor', 'facecolors', 'linewidths', 's', 'x', 'y'.


备注

1. 感谢老师的教学与课件  
2. 欢迎各位同学一起来交流学习心得^_^  
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。  


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。