matplotlib.pyplot.plot详解

  参考资料:

  https://blog.csdn.net/baidu_41902768/article/details/80686608

  之前的随笔也有说过,matplotlib是python中一个非常常用的用来作图的库,pyplot是其中的一个包,主要是用来作2D图的,涉及的画布,图例,标签等一系列作图常规操作。这篇文章分析的是pyplot这个包下面的一个最常用的函数plot,所以使用的时候我们经常是:

import matplotlib.pyplot as plt
plt.plot()

其中把pyplot简写成plt已经成为一种习惯。因为经常会用到这个函数作图,又看了matplotlib的文档,写的非常好。所以就想学习一下记录在此。参考资料那篇文章其实做的是一样的事情,但是为了自己熟悉一遍还是一步步学习一下。

  函数定义:(Plot y versus x as lines and/or markers 说的很清楚很局限,这个函数就是绘制2D图 x versus y的,线图或者点图,别的干不了。)

matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
plot([x], y, [fmt], *, data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

  上面的[fmt]其实就是一个字符串,方便你格式化作图。比如'bo'='blue circle',蓝色圆点。'r--'就是红色的短线图。上面的第三行就是一次性同时画两个图时的用法。

>>> plot(x, y)        # plot x and y using default line style and color
>>> plot(x, y, 'bo')  # plot x and y using blue circle markers
>>> plot(y)           # plot y using x as index array 0..N-1
>>> plot(y, 'r+')     # ditto, but with red plusses

  还可以使用关键字参数,可以和fmt共存,当fmt和关键字冲突时,关键字优先:

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

  

  带标签的数据作图:(Plotting labelled data):原文是这样的There's a convenient way for plotting objects with labelled data (i.e. data that can be accessed by index obj['y']). Instead of giving the data in x and y, you can provide the object in the data parameter and just give the labels for x and y:

plot('xlabel', 'ylabel', data=obj)

  带索引的对象可以是dict,pandas.DataFame,structured numpy array.个人理解这个用法的作用就是:当你有一个表格,其中有很多列,每一个列有一个标签。当你想使用其中的两列作图的时候,将表格整体传入,但是只要你指定了标签,这个函数就会帮你把这两列取出来作图。不妨用支持的最简单类型dict做个试验。

>>>A = {'1':[1,2,3,4,5] , '2':[1,2,3,4,5] ,'3':[2,3,4,5,6]}
>>>plt.plot('1','2','r',data=A)

  和预想的一样,结果就是取出字典中key为'1'和'2'的两个列表分别为x和y进行作图。

  

  多组数据作图:一共有三种方法

  1.最直接,多次调用plot函数:

>>> plot(x1, y1, 'bo')
>>> plot(x2, y2, 'go')

  2.如果数据已经是2d array,可以直接传入。例:一个数组,第一列代表x values ,其他多个列代表y values。

>>> plot(a[0], a[1:])

  3.明确指定多个数据集:这种情况下关键字参数将用于所有的数据集。

>>> plot(x1, y1, 'g^', x2, y2, 'g-')

  

Notes:有关作图格式的一些信息。

Format Strings

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

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

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.

Other combinations such as [color][marker][line] are also supported, but note that their parsing may be ambiguous.

Markers

characterdescription
'.' 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

characterdescription
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style

Example format strings:

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

The supported color abbreviations are the single letter codes

charactercolor
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white

and the 'CN' colors that index into the default property cycle.

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').

posted @ 2019-11-18 17:23  思念殇千寻  阅读(1623)  评论(0编辑  收藏  举报