matplotlib API入门
plot(x, y, 'go--', linewidth=2, markersize=12) # color marker linsstyle
**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
============= ===============================
**Colors**
The supported color abbreviations are the single letter codes
============= =============================== # 颜色也可以是16进制的颜色代码
character color
============= ===============================
``'b'`` blue
``'g'`` green
``'r'`` red
``'c'`` cyan
``'m'`` magenta
``'y'`` yellow
``'k'`` black
``'w'`` white
============= ===============================
1.1 图片与子片
matplotlib所绘制的图位于图片(Figure)对象中,,可以使用 plt.figure生成一个新的图片:fig=plt.figure()
就会出现一个空的绘图窗口,我们可以使用一些命令来显示,比如 figsize 确保图片有一个确定的大小以及储存到硬盘时的长宽比。
figure参数 | 含义 |
---|---|
num( int or str) | 图形的编号,默认是递增的,如果编号存在,就是对其的引用,如果是字符串,图形编号就是这个 |
figsize(float,float) | 宽度,高度,单位英寸。 |
dpi | 默认100,分辨率 |
facecolor | 背景颜色 |
edgecolor | 边框颜色 |
frameon | 布尔值,默认True, 如果False,禁止绘制图形框。 |
不能使用空白的图片进行绘图,需要使用add_subplot 创建一个或多个子图(subplot) ax1=fig.add_subplolt(2,2,1)
意思是图片应该是 2X2 的(最多四个图形),并且我们选择了四个图形中的第一个(序号从1开始)。如果你接着创建了多个子图,你会看到:
(jupyter notebook 的每个单元格运行后,图标都会重置,对复杂的图标,必须将所有的绘图命令放到单个单元格中。)额,,把最后那个ax3=fig.add_subplot(2,2,4) 放到下一个格子运行,还是会放到这个图片里的,可以自己试试啊。。。。
输入 plt.plot() 命令会在最后一个图片和子图上进行绘制
’ k–’ 是用于绘制黑色虚线的 style 选项,fig.add_subplot 返回的对象是Axes Subplot 对象,使用这些对象你可以直接在其他空白的子图上调用对象的实例方法进行绘图。
就是柱状图和离散分布,,完整图形类型去看 matplotlib 的文档吧,,,脑补连接
plt.subplots
创建一个新的图片,然后返回包含了已生成子图对象的numpy数组,
这是很有用的,因为axes 可以像二维数组那样进行索引,axes[0,1]
也可以通过使用 sharex and sharey 来表明子图分别拥有相同的x y 轴。
pyplot.subplots 参数 | 描述 |
---|---|
nrows | 子图的行数 |
ncols | 子图的列数 |
sharex | 所有子图使用相同的x轴刻度(调整 xlim 会影响所有子图) |
sharey | 所有子图使用相同的 y轴刻度(ylim调整会影响所有子图) |
subplot_kw | 传入 add_subplot 的关键字参数字典,用于生成子图(add_subplot的参数还真不少。。) |
**fig_kw | 在生成图片时使用的额外关键字参数,例如 plt.subplots(2,2,figsize=(8,6)) |
1.1.1 调整子图周围的间距
默认情况下,会在子图的外部和子图之间留出一定间距,这个间距时根据图的高度和宽度调整的,
subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=None,hspace=None)
调整间距
wspace,hspace 控制的是图片的宽度和高度的百分比,以用作子图间的间距