欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

matplotlib库学习(一)

程序员文章站 2022-03-21 14:21:13
...

matplotlib库是python中实现数据处理与展示的非常优秀的类库, 它提供了超过100多种的图像处理和现实方法 可以说是非常强大了

我们来看看这个函数:

import tensorflow as tf 
import numpy as np 
import matplotlib.pyplot as plt 

train_X = np.linspace(-1, 1, 100) # x 从-1 ~ 1 均匀分布100个样本,默认包括start&endpoint
train_Y = 2 * train_X + np.random.randn(*train_X.shape) * 0.3

plt.plot(train_X, train_Y, 'ro', label = "Original data")#Plot y versus x as lines and/or markers.

plt.plot这句有很强的拓展性,可以根据不同的参数刻画出不同的图像

下面我们来看看这些参数:

train_x : x

train_y: y

The coordinates of the points or line nodes are given by x, y.

x,y 还可以通过标签给定:

使用方法如下:

 plot('xlabel', 'ylabel', data=obj)# obj['xlabel'] 

All indexable objects are supported. This could e.g. be a dict, a pandas.DataFame or a structured numpy array.

当要绘制多组x, y, 我们可以这样给定参数:

  • The most straight forward way is just to call plot multiple times.
    Example:

      >>> plot(x1, y1, 'bo')
      >>> plot(x2, y2, 'go')
    
  • Alternatively, if your data is already a 2d array, you can pass it
    directly to x, y. A separate data set will be drawn for every
    column.

       Example: an array ``a`` where the first column represents the *x*
       values and the other columns are the *y* columns::
    
       >>> plot(a[0], a[1:])
    
  • The third way is to specify multiple sets of [x], y, [fmt]
    groups::

        >>> plot(x1, y1, 'g^', x2, y2, 'g-')
    
        In this case, any additional keyword argument applies to all
        datasets. Also this syntax cannot be combined with the *data*
        parameter.
    

‘ro’ : fmt

The optional parameter fmt is a convenient way for defining basic formatting like color, marker and linestyle.

让我们看具体了解一下这个参数;

"""
Notes
        -----
        **Format Strings**

        A format string consists of a part for color, marker and line::

            fmt = '[color][marker][line]'

        Each of them is optional. If not provided, the value from the style
        cycle is used. Exception: If ``line`` is given, but no ``marker``,
        the data will be a line without markers.

        **Colors**

        The following color abbreviations are supported:

        =============    ===============================
        character        color
        =============    ===============================
        ``'b'``          blue
        ``'g'``          green
        ``'r'``          red
        ``'c'``          cyan
        ``'m'``          magenta
        ``'y'``          yellow
        ``'k'``          black
        ``'w'``          white
        =============    ===============================

        If the color is the only part of the format string, you can
        additionally use any  `matplotlib.colors` spec, e.g. full names
        (``'green'``) or hex strings (``'#008000'``).

        **Markers**

        =============    ===============================
        character        description
        =============    ===============================
        ``'.'``          point marker
        ``','``          pixel marker
        ``'o'``          circle marker
        ``'v'``          triangle_down marker
        ``'^'``          triangle_up marker
        ``'<'``          triangle_left marker
        ``'>'``          triangle_right marker
        ``'1'``          tri_down marker
        ``'2'``          tri_up marker
        ``'3'``          tri_left marker
        ``'4'``          tri_right marker
        ``'s'``          square marker
        ``'p'``          pentagon marker
        ``'*'``          star marker
        ``'h'``          hexagon1 marker
        ``'H'``          hexagon2 marker
        ``'+'``          plus marker
        ``'x'``          x marker
        ``'D'``          diamond marker
        ``'d'``          thin_diamond marker
        ``'|'``          vline marker
        ``'_'``          hline marker
        =============    ===============================

        **Line Styles**

        =============    ===============================
        character        description
        =============    ===============================
        ``'-'``          solid line style
        ``'--'``         dashed line style
        ``'-.'``         dash-dot line style
        ``':'``          dotted line style
        =============    ===============================
"""

eg:

""" 
        Example format strings::

            'b'    # blue markers with default shape
            'ro'   # red circles
            'g-'   # green solid line
            '--'   # dashed line with default color
            'k^:'  # black triangle_up markers connected by a dotted line
"""

还有一种控制 appearance 的方法 :

"""
   You can use `.Line2D` properties as keyword arguments for more
        control on the  appearance. Line properties and *fmt* can be mixed.
        The following two calls yield identical results:

        >>> plot(x, y, 'go--', linewidth=2, markersize=12)
        >>> plot(x, y, color='green', marker='o', linestyle='dashed',
                linewidth=2, markersize=12)
"""

值得注意的是, 当两者有冲突的时候, 后者占主导地位