概览
-
The pyplot API
matplotlib.pyplot 是一个像Matlab一样工作的命令集合,每一个pyplot函数都对图标做出一些变更: 创建图表/创建图表绘制区域/绘制线条/绘制标签,`pyplot 的目的是交互式绘制和简单的自动化绘制。
-
The object-oriented API
Matplotlib的核心是面向对象的,如果需要更多的控制和自定义绘制,我们建议直接使用对象工作。
很多情况下,你可以使用pyplot.subplots创建一个Figure和很多的Axes, 然后使用这些对象工作,同样也可以显式的创建Figure(GUI应用中)。
-
The pylab API(不赞成)
matplotlib.pylab模块在一个单独的命名空间包含 matplotlib.pyplot, numpy 和 其他的函数,初始目的是 通过导入所有的函数到全局命名空间来模仿一个类Matlab的工作方式。
API
-
图表 matplot.figure.Figure
所有绘图元素最顶层的容器
图表实例支持回调,通过callbacks(CallbackRegistry实例) 属性
import matplotlib.pyplot as plt
# 创建一个Figure
fig = plt.figure()
plt.plot()
plt.show()
-
plot
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)
点或线的坐标通过x/y给出,
可选参数fmt是一个字符串标识,可以方便的定义颜色、标记、线型
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
可以使用Line2D属性作为关键字在外形上控制更多,线属性和fmt可以混合使用
plot(x, y, 'go--', linewidth=2, markersize=12)`
plot(x, y, color='green', marker='o', linestyle='dashed', linewidth=2, markersize=12)
使用fmt时,关键字参数优先生效
参数
x y
可以是数组或整数,x是可选的,若无值则默认[0 ,..., N-1]
fmt
可选的字符串,是快速设置线的属性的一个缩写,所有属性均可以由关键字参数控制
data
可索引对象,可选,标签数据对象,提供标签名称以绘制x y 坐标轴
返回值
lines
代表绘制数据的Line2D对象列表
fmt
包含颜色、标记、线的格式化字符串,每一项都是可选的,如果没有提供某项则使用周期循环中的值
fmt = '[color][marker][line]'
color 支持的颜色
| character | color |
|---|---|
| 'b' | blue |
| 'g' | green |
| 'r' | red |
| 'c' | cyan |
| 'm' | magenta |
| 'y' | yellow |
| 'k' | black |
| 'w' | white |
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 Style
| character | description |
|---|---|
| '-' | solid line style |
| '--' | dashed line style |
| '-.' | dash-dot line style |
| ':' | dotted line style |
-
subplots
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False,squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
创建一个图表和一组绘图区域
nrows, ncols 绘图区域网格的行列,默认1行1列
sharex, sharey: bool or {'none', 'all', 'row', 'col'}, default: False 控制多个绘图区域是否共用x、y坐标轴属性
-
Axes
class matplotlib.axes.Axes(fig, rect, facecolor=None, frameon=True, sharex=None, sharey=None, label='', xscale=None, yscale=None, **kwargs)
Axes 包含很多元素:坐标轴、刻度、2D线、文本、多边形









网友评论