参考资料:

  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

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

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

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

matplotlib.pyplot.plot详解的更多相关文章

  1. python matplotlib.pyplot 散点图详解(1)

    python matplotlib.pyplot散点图详解(1) 一.创建散点图 可以用scatter函数创建散点图 并使用show函数显示散点图 代码如下: import matplotlib.py ...

  2. python matplotlib.pyplot 条形图详解

    python matplotlib.pyplot 条形图详解 一.创建直方图 可以用bar函数来创建直方图 然后用show函数显示直方图 比如: import matplotlib.pyplot as ...

  3. python matplotlib.pyplot 散点图详解(2)

    python matplotlib.pyplot 散点图详解(2) 上期资料 一.散点图叠加 可以用多个scatter函数叠加散点图 代码如下: import matplotlib.pyplot as ...

  4. matplotlib.pyplot 绘图详解 matplotlib 安装

    apt-get install python-matplotlib 转载自: http://www.cnblogs.com/qianlifeng/archive/2012/02/13/2350086. ...

  5. matplotlib极坐标方法详解

    一.极坐标 在平面内取一个定点O,叫极点,引一条射线Ox,叫做极轴,再选定一个长度单位和角度的正方向(通常取逆时针方向).对于平面内任何一点M,用ρ表示线段OM的长度(有时也用r表示),θ表示从Ox到 ...

  6. matplotlib 直方图绘制详解

    n, bins, patches = plt.hist(datasets, bins, normed=False, facecolor=None, alpha=None) 函数说明 用于绘制多个数据集 ...

  7. python matplotlib.pyplot学习记录

    matplotlib是python中很强大的绘图工具,在机器学习中经常用到 首先是导入 import matplotlib.pyplot as plt plt中有很多方法,记录下常用的方法 plt.p ...

  8. matplotlib作图——plot() 线图

    线图 #定义 matplotlib.pyplot.plot() plot([x], y, [fmt], data=None, **kwargs) plot([x], y, [fmt], [x2], y ...

  9. scikit-learn:matplotlib.pyplot经常使用绘图功能总结(1)

    參考:http://matplotlib.org/api/pyplot_api.html 绘图功能总结(2):http://blog.csdn.net/mmc2015/article/details/ ...

随机推荐

  1. MySql 常用的函数

    一.聚合函数 avg(col)      计算平均值    count(col)    计算中非NULL值的个数(这个容易忘)    greatest(x1,x2,...,xn) 返回集合中最大的值  ...

  2. Mybaties 的缓存

    1.mybaties 默认会话是一级缓存,没有开启二级缓存.开启方式在mapper文件中设置<cache /> 2.<cache eviction="LRU" f ...

  3. Python一键获取日漫Top100榜单电影信息

    最近看到一个 UP 主做的视频,使用可视化动态图,把目前播放量最多的 UP 主一一列出来,结果第一名是哔哩哔哩番剧,第一名的播放量是第二名近 10 倍. B站的番剧数量,也是相对其他平台比较多的,而且 ...

  4. nodejs模块介绍与使用

    1.内置模块 http fs path url os等等 (无需安装直接require引入即可) 2.第三方模块: express mysql ejs等等 (npm包管理工具下载require引入) ...

  5. [Objective-C] 005_Category(类别)

    Category的实际作用就是为已有的类来添加方法.为现有的类添加的方法可以先不用实现,在需要的时候再实现也是可以的.在我们的实际代码中如何来实现Category的呢?我们上篇的Person 类为例. ...

  6. Java集合(十)实现Map接口的HashMap

    Java集合(十)继承Map接口的HashMap 一.HashMap简介(基于JDK1.8) HashMap是基于哈希表(散列表),实现Map接口的双列集合,数据结构是“链表散列”,也就是数组+链表 ...

  7. display有哪些值?说明他们的作用?

    inline(默认)— 内联 none — 隐藏 block — 显示.块级元素(单独占一行) inline-block — 行内块元素(不占整行) table — 表格显示 list-item — ...

  8. Spring Boot笔记(二) springboot 集成 SMTP 发送邮件

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 笔记:乘着项目迭代的间隙,把一些可复用的功能从项目中抽取出来,这是其中之一, 一.添加SMTP 及 MA ...

  9. Java实现蓝桥杯算法提高P0102

    算法提高 P0102 时间限制:1.0s 内存限制:256.0MB 提交此题 用户输入三个字符,每个字符取值范围是0-9,A-F.然后程序会把这三个字符转化为相应的十六进制整数,并分别以十六进制,十进 ...

  10. Java实现 蓝桥杯VIP 基础练习 芯片测试

    问题描述 有n(2≤n≤20)块芯片,有好有坏,已知好芯片比坏芯片多. 每个芯片都能用来测试其他芯片.用好芯片测试其他芯片时,能正确给出被测试芯片是好还是坏.而用坏芯片测试其他芯片时,会随机给出好或是 ...