可视化学习
一、matplotlib 画图
1.matplotlib 三层API
matplotlib.backend_bases.FigureCanvas
代表了绘图区,所有的图像都是在绘图区完成的
matplotlib.backend_bases.Renderer
代表了渲染器,可以近似理解为画笔,控制如何在 FigureCanvas 上画图。
matplotlib.artist.Artist
代表了具体的图表组件,即调用了Renderer的接口在Canvas上作图。
前两者处理程序和计算机的底层交互的事项,第三项Artist就是具体的调用接口来做出我们想要的图,比如图形、文本、线条的设定。所以通常来说,我们95%的时间,都是用来和matplotlib.artist.Artist类打交道的。
2.Artist 的分类
Artist有两种类型:primitives
和containers
。
primitive
是基本要素,它包含一些我们要在绘图区作图用到的标准图形对象,如曲线Line2D,文字text,矩形Rectangle,图像image等。
container
是容器,即用来装基本要素的地方,包括图形figure
、坐标系Axes
和坐标轴Axis
。他们之间的关系如下图所示:
matplotlib的画图流程
1.创建一个Figure实例
2.使用Figure实例创建一个或者多个Axes或Subplot实例
3.使用Axes实例的辅助方法来创建primitive
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
axes = fig.add_subplot(2,1,1)
t= np.arange(0.0,4.0,0.01)
s = np.cos(2*np.pi*t)
line, = axes.plot(t,s,color="blue",lw=2)
二、自定义你的Artist对象
1.Artist属性
在图形中的每一个元素都对应着一个matplotlib Artist,且都有其对应的配置属性列表。
Figure
本身包含一个Rectangle,Rectangle的大小就是Figure的大小;你可以用来设置Figure的背景色和透明度。
每个Axes边界框(默认白底黑边),也有一个Rectangle,通过它可以设置Axes的颜色、透明度等。
这些实例都存储在成员变量(member variables) Figure.patch
和 Axes.patch
中。 (Patch是一个来源于MATLAB的名词,它是图形上颜色的一个2D补丁,包含rectangels-矩形,circles-圆 和 plygons-多边形)
换个表达方式:
Figure.patch
属性:是一个Rectangle,代表了图表的矩形框,它的大小就是图表的大小, 并且可以通过它设置figure的背景色和透明度。
Axes.patch
属性:也是一个Rectangle,代表了绘图坐标轴内部的矩形框(白底黑边), 通过它可以设置Axes的颜色、透明度等。
每个matplotlib Artist都有以下属性:
alpha
属性:透明度。值为0—1之间的浮点数
axes
属性:返回这个Artist所属的axes,可能为None
figure
属性:该Artist所属的Figure,可能为None
label
:一个text label
visible
:布尔值,控制Artist是否绘制
plt.figure().patch
plt.axes().patch
2. 属性调用方式
matplotlib.artist.getp()
来获取属性,如果指定属性名,则返回对象的该属性值;如果不指定属性名,则返回对象的所有的属性和值。
import matplotlib
# Figure rectangle的属性
matplotlib.artist.getp(fig.patch)
agg_filter = None
alpha = None
animated = False
antialiased or aa = False
bbox = Bbox(x0=0.0, y0=0.0, x1=1.0, y1=1.0)
capstyle = butt
children = []
clip_box = None
clip_on = True
clip_path = None
contains = None
data_transform = BboxTransformTo( TransformedBbox( Bbox…
edgecolor or ec = (1.0, 1.0, 1.0, 0.0)
extents = Bbox(x0=0.0, y0=0.0, x1=432.0, y1=288.0)
facecolor or fc = (1.0, 1.0, 1.0, 0.0)
figure = Figure(432x288)
fill = True
gid = None
hatch = None
height = 1
in_layout = True
joinstyle = miter
label =
linestyle or ls = solid
linewidth or lw = 0.0
patch_transform = CompositeGenericTransform( BboxTransformTo( …
path = Path(array([[0., 0.], [1., 0.], [1.,…
path_effects = []
picker = None
rasterized = None
sketch_params = None
snap = None
transform = CompositeGenericTransform( CompositeGenericTra…
transformed_clip_path_and_affine = (None, None)
url = None
verts = [[ 0. 0.] [432. 0.] [432. 288.] [ 0. 288…
visible = True
width = 1
window_extent = Bbox(x0=0.0, y0=0.0, x1=432.0, y1=288.0)
x = 0
xy = (0, 0)
y = 0
zorder = 1
三、基本元素 - primitives
1.2DLines
在matplotlib中曲线的绘制,主要是通过类 matplotlib.lines.Line2D 来完成的。
它的基类: matplotlib.artist.Artist
matplotlib中线-line
的含义:它表示的可以是连接所有顶点的实线样式,也可以是每个顶点的标记。此外,这条线也会受到绘画风格的影响,比如,我们可以创建虚线种类的线。
它的构造函数:
class matplotlib.lines.Line2D(xdata, ydata, linewidth=None, linestyle=None, color=None, marker=None, markersize=None, markeredgewidth=None, markeredgecolor=None, markerfacecolor=None, markerfacecoloralt=‘none’, fillstyle=None, antialiased=None, dash_capstyle=None, solid_capstyle=None, dash_joinstyle=None, solid_joinstyle=None, pickradius=5, drawstyle=None, markevery=None, **kwargs)
常见的参数:
- xdata:需要绘制的line中点的在x轴上的取值,若忽略,则默认为 range(1,len(ydata)+1)
- ydata:需要绘制的line中点的在y轴上的取值
- linewidth:线条的宽度
- linestyle:线型
- color:线条的颜色
- ma