绘图:Matplotlib

本文介绍了使用 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:

markerdescription
”.”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
TICKLEFTtickleft
TICKRIGHTtickright
TICKUPtickup
TICKDOWNtickdown
CARETLEFTcaretleft
CARETRIGHTcaretright
CARETUPcaretup
CARETDOWNcaretdown
“None”nothing
Nonenothing
” “nothing
“”nothing
'$...$'render the string using mathtext.
vertsa list of (x, y) pairs used for Path vertices. The center of the marker is located at (0,0) and the size is normalized.
pathPath instance.
(numsidesstyleangle)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 antialising 
  • Use 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.

 

PropertyValue Type
alphafloat
animated[True | False]
antialiased or aa[True | False]
clip_boxa matplotlib.transform.Bbox instance
clip_on[True | False]
clip_patha Path instance and a Transform instance, a Patch
color or cany matplotlib color
containsthe hit testing function
dash_capstyle['butt' | 'round' | 'projecting']
dash_joinstyle['miter' | 'round' | 'bevel']
dashessequence of on/off ink in points
data(np.array xdata, np.array ydata)
figurea matplotlib.figure.Figure instance
labelany string
linestyle or ls'-' | '--' | '-.' | ':' | 'steps' | ...]
linewidth or lwfloat value in points
lod[True | False]
marker'+' | ',' | '.' | '1' | '2' | '3' | '4' ]
markeredgecolor or mecany matplotlib color
markeredgewidth or mewfloat value in points
markerfacecolor or mfcany matplotlib color
markersize or msfloat
markevery[ None | integer | (startind, stride) ]
pickerused in interactive line selection
pickradiusthe line pick selection radius
solid_capstyle['butt' | 'round' | 'projecting']
solid_joinstyle['miter' | 'round' | 'bevel']
transforma matplotlib.transforms.Transform instance
visible[True | False]
xdatanp.array
ydatanp.array
zorderany number

转载于:https://www.cnblogs.com/lailailai/p/4580731.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值