绘图:Matplotlib
用于绘制一些数据图,同学推荐的,挺好用。非常好的官网文档:http://matplotlib.org/contents.html
0. 安装
可以直接pip install,还有一些依赖就按照提示来吧,具体也忘了。
1. 基本画图
import matplotlib.pyplot as plt
xs = [1,2,3,4]
ys = [1,2,3,4]
plt.plot(xs, ys)
plt.show()
xs表示点的x坐标,ys表示点的y坐标,所画的点就是(xs[i], ys[i]),默认情况下会依次用直线把点连起来。此时可以看到弹出一张过原点斜率为1的直线。注意show过后,不能再次show了,需要再plot画一次。说明这个plot模块是有状态的。
2. 散点
可以设置画图点的模式(markers),不使用直线将点连起来,而就是画个散点即可:
import matplotlib.pyplot as plt
xs = [1,2,3,4]
ys = [1,2,3,4]
plt.plot(xs, ys, "ob");
plt.show()
此时看到的就是几个离散的点。这里"ob"表示使用圆形的marker并且颜色是蓝色(blue),其中filled_markers可以是:
'o' - 圆点, 'v' - 倒三角, '^' - 正三角, '<' - 左三角, '>' - 右三角, '8', 's' - 正方形, 'p' - 凸五边形, '*' - 五角星, 'h', 'H', 'D' - 菱形, 'd' - 扁菱形
All possible markers are defined here:
| marker | description |
|---|---|
| ”.” | point |
| ”,” | pixel |
| “o” | circle |
| “v” | triangle_down |
| “^” | triangle_up |
| “<” | triangle_left |
| “>” | triangle_right |
| “1” | tri_down |
| “2” | tri_up |
| “3” | tri_left |
| “4” | tri_right |
| “8” | octagon |
| “s” | square |
| “p” | pentagon |
| “*” | star |
| “h” | hexagon1 |
| “H” | hexagon2 |
| “+” | plus |
| “x” | x |
| “D” | diamond |
| “d” | thin_diamond |
| “|” | vline |
| “_” | hline |
| TICKLEFT | tickleft |
| TICKRIGHT | tickright |
| TICKUP | tickup |
| TICKDOWN | tickdown |
| CARETLEFT | caretleft |
| CARETRIGHT | caretright |
| CARETUP | caretup |
| CARETDOWN | caretdown |
| “None” | nothing |
| None | nothing |
| ” “ | nothing |
| “” | nothing |
| '$...$' | render the string using mathtext. |
| verts | a list of (x, y) pairs used for Path vertices. The center of the marker is located at (0,0) and the size is normalized. |
| path | a Path instance. |
| (numsides, style, angle) | see below |
http://matplotlib.org/api/markers_api.html#module-matplotlib.markers
3. 填充数据
可以结合numpy来快速的填充数据,画出图形
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x=np.arange(0, 4, 0.05)
>>> x
array([ 0. , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 ,
0.45, 0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85,
0.9 , 0.95, 1. , 1.05, 1.1 , 1.15, 1.2 , 1.25, 1.3 ,
1.35, 1.4 , 1.45, 1.5 , 1.55, 1.6 , 1.65, 1.7 , 1.75,
1.8 , 1.85, 1.9 , 1.95, 2. , 2.05, 2.1 , 2.15, 2.2 ,
2.25, 2.3 , 2.35, 2.4 , 2.45, 2.5 , 2.55, 2.6 , 2.65,
2.7 , 2.75, 2.8 , 2.85, 2.9 , 2.95, 3. , 3.05, 3.1 ,
3.15, 3.2 , 3.25, 3.3 , 3.35, 3.4 , 3.45, 3.5 , 3.55,
3.6 , 3.65, 3.7 , 3.75, 3.8 , 3.85, 3.9 , 3.95])
>>> plt.plot(x, np.sin(0.5 * np.pi * x))
[<matplotlib.lines.Line2D object at 0x11314a810>]
>>> plt.show()
4. 线条样式
Controlling line properties
Lines have many attributes that you can set: linewidth, dash style, antialiased, etc; see matplotlib.lines.Line2D. There are several ways to set line properties
Use keyword args:
plt.plot(x, y, linewidth=2.0)
Use the setter methods of the Line2D instance. plot returns a list of lines; e.g., line1, line2 = plot(x1,y1,x2,y2). Below I have only one line so it is a list of length 1. I use tuple unpacking in the line, = plot(x, y, 'o') to get the first element of the list:
line, = plt.plot(x, y, '-')
line.set_antialiased(False) # turn off antialisingUse the setp() command. The example below uses a MATLAB-style command to set multiple properties on a list of lines. setpworks transparently with a list of objects or a single object. You can either use python keyword arguments or MATLAB-style string/value pairs:
lines = plt.plot(x1, y1, x2, y2)
# use keyword args
plt.setp(lines, color='r', linewidth=2.0)
# or MATLAB style string value pairs
plt.setp(lines, 'color', 'r', 'linewidth', 2.0)
Here are the available Line2D properties.
| Property | Value Type |
|---|---|
| alpha | float |
| animated | [True | False] |
| antialiased or aa | [True | False] |
| clip_box | a matplotlib.transform.Bbox instance |
| clip_on | [True | False] |
| clip_path | a Path instance and a Transform instance, a Patch |
| color or c | any matplotlib color |
| contains | the hit testing function |
| dash_capstyle | ['butt' | 'round' | 'projecting'] |
| dash_joinstyle | ['miter' | 'round' | 'bevel'] |
| dashes | sequence of on/off ink in points |
| data | (np.array xdata, np.array ydata) |
| figure | a matplotlib.figure.Figure instance |
| label | any string |
| linestyle or ls | [ '-' | '--' | '-.' | ':' | 'steps' | ...] |
| linewidth or lw | float value in points |
| lod | [True | False] |
| marker | [ '+' | ',' | '.' | '1' | '2' | '3' | '4' ] |
| markeredgecolor or mec | any matplotlib color |
| markeredgewidth or mew | float value in points |
| markerfacecolor or mfc | any matplotlib color |
| markersize or ms | float |
| markevery | [ None | integer | (startind, stride) ] |
| picker | used in interactive line selection |
| pickradius | the line pick selection radius |
| solid_capstyle | ['butt' | 'round' | 'projecting'] |
| solid_joinstyle | ['miter' | 'round' | 'bevel'] |
| transform | a matplotlib.transforms.Transform instance |
| visible | [True | False] |
| xdata | np.array |
| ydata | np.array |
| zorder | any number |
绘图:Matplotlib的更多相关文章
- Python绘图matplotlib
转自http://blog.csdn.net/ywjun0919/article/details/8692018 Python图表绘制:matplotlib绘图库入门 matplotlib 是pyth ...
- 绘图 Matplotlib Numpy Pandas
丈夫气力全,一个拟当千.猛气冲心出,视死亦如眠. 绘图 Matplotlib可视化是在整个数据挖掘的关键辅助工具,可以清晰的理解数据,从而调整我们的分析方法. 能将数据进行可视化,更直观的呈现使数据更 ...
- 爬虫之绘图matplotlib与词云(七)
1 绘制条形图 import matplotlib # 数据可视化 from matplotlib import pyplot as plt # 配置字体 matplotlib.rcParams[&q ...
- python绘图 matplotlib教程
mark一个很好的python绘图教程 https://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/
- 绘图matplotlib
前言 matplotlib是python的一个绘图库,如果你没有绘制过图,可以先试试js的绘图库http://www.runoob.com/highcharts/highcharts-line-lab ...
- 机器学习——可视化绘图matplotlib和seaborn
安装matplotlib和seaborn https://blog.csdn.net/Jia_jinjin/article/details/80428598 seaborn pairplot:特征两两 ...
- 绘图: matplotlib核心剖析
参考:http://www.cnblogs.com/vamei/archive/2013/01/30/2879700.html http://blog.csdn.net/ywjun0919/artic ...
- matplotlib 绘图
http://blog.csdn.net/jkhere/article/details/9324823 都打一遍 5 matplotlib-绘制精美的图表 matplotlib 是python最著名的 ...
- matplotlib绘图基本用法-转自(http://blog.csdn.net/mao19931004/article/details/51915016)
本文转载自http://blog.csdn.net/mao19931004/article/details/51915016 <!DOCTYPE html PUBLIC "-//W3C ...
- 使用 Python 的 matplotlib 绘图库进行绘图
matplotlib 是 Python 最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 1 使用 Ma ...
随机推荐
- 牛客第二场 C.message(计算几何+二分)
题目传送:https://www.nowcoder.com/acm/contest/140/C 题意:有n个云层,每个云层可以表示为y=ax+b.每个飞机的航线可以表示为时间x时,坐标为(x,cx+d ...
- 纯css实现打字效果
概述 很早以前就在别人的博客上面看到打字动画了,觉得非常炫酷,以为是用js做的,找了半天也没找到js在哪里.今天看<css揭秘>,碰巧看到书上打字动画的实现了,而且是纯css实现的!我参考 ...
- spring cloud 学习(6) - zuul 微服务网关
微服务架构体系中,通常一个业务系统会有很多的微服务,比如:OrderService.ProductService.UserService...,为了让调用更简单,一般会在这些服务前端再封装一层,类似下 ...
- Linux - 更改软件源
镜像源 网易镜像源 在网易开源镜像页面,点击对应镜像名的使用帮助,可以查看到更新源的方法,按步骤操作即可. 阿里云镜像源 在阿里云开源镜像页面,点击对应Mirror分类的help标签,可以查看到更新源 ...
- Flask-WTF
Flask-WTF 提供了简单地 WTForms 的集成. 官方文档:http://www.pythondoc.com/flask-wtf/index.html 功能 集成 wtforms. 带有 c ...
- CentOS 6(64-bit) + Nginx搭建静态文件服务器
Nginx搭建静态文件服务器 使用命令打开Nginx配置文件: sudo vim /etc/nginx/conf.d/default.conf 将配置改为: server { ...... ..... ...
- 性能优化中CPU、内存、磁盘IO、网络性能的依赖(转)
关于系统性能优化,推荐一篇不错的博客! 系统优化是一项复杂.繁琐.长期的工作,优化前需要监测.采集.测试.评估,优化后也需要测试.采集.评估.监测,而且是一个长期和持续的过程,不 是说现在优化了,测试 ...
- PCA (主成分分析)详解 (写给初学者) 结合matlab(转载)
一.简介 PCA(Principal Components Analysis)即主成分分析,是图像处理中经常用到的降维方法,大家知道,我们在处理有关数字图像处理方面的问题时,比如经常用的图像的查询问题 ...
- @RequestParam注解
SpringMVC的参数指定注解:@RequestParam,有下面四个方法: value 参数绑定,value里写的是URL里参数名称 name 同上 required 是否必需参数,默认为tr ...
- 【源码分析】HashMap源码再读-基于Java8
最近工作不是太忙,准备再读读一些源码,想来想去,还是先从JDK的源码读起吧,毕竟很久不去读了,很多东西都生疏了.当然,还是先从炙手可热的HashMap,每次读都会有一些收获.当然,JDK8对HashM ...