Python数据可视化-matplotlib学习1

介绍:

matplotlib是Python著名的绘图库,提供了一整套和matlib相似的命令API,适合交互式制图,或者,将它作为绘图控件嵌入到GUI应用程序中。

pyplot子库:

该子库提供了类似matlib的绘图API,可以方便的绘制2D图表。

使用过程:

1.导入pyplot子库

import matplotlib.pyplot as plt

2.创建figure对象

plt.figure(figsize=(8,4))

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)

parameters

num:该figure对象的编号,默认为None,即可以不提供该参数,如果不提供,则在后面的plot函数中直接绘图,

matplotlib会自动创建一个figure对象,并增加matplotlib的编号,如果提供该参数,并且该编号的figure对象存在,

就启用这个绘图对象,如果该对象不存在,那么就创建该对象并启用它。如果提供的是String形式,那么title将被

设置为绘图对象的编号。

figsize:指定绘图对象的宽度和高度,单位为英寸。

dpi:绘图对象的分辨率,即每英寸多少像素,缺省值为80.如果没有提供,缺省值为rc figure.dpi,如下方式查看。

import matplotlib
matplotlib.rcParams["figure.dpi"]

facecolor :

the background color. If not provided, defaults to rc figure.facecolor.

edgecolor :

the border color. If not provided, defaults to rc figure.edgecolor.

frameon : bool, optional, default: True

If False, suppress drawing the figure frame.

FigureClass : class derived from matplotlib.figure.Figure

Optionally use a custom Figure instance.

clear : bool, optional, default: False

If True and the figure already exists, then it is cleared.

3.调用plot函数进行绘图

plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2)
plt.plot(x,z,"b--",label="$cos(x^2)$")
matplotlib.pyplot. plot ( *args, **kwargs )

args,表示可变长度的参数集,即提供几个值都正确。

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

如果x或y是二维的,那么相应的列也会被绘制出来。

  • 上例中的'bo'和'r+'都是样式指定,这是样式指定的一种方式。

样式表如下:

The following format string characters are accepted to controlthe line style or marker:

characterdescription
'-'solid line style
'--'dashed line style
'-.'dash-dot line style
':'dotted line style
'.'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

The following color abbreviations are supported:

charactercolor
‘b’blue
‘g’green
‘r’red
‘c’cyan
‘m’magenta
‘y’yellow
‘k’black
‘w’white

  • 另一种方式是提供关键字参数,即

plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
plot([1,2,3], [1,4,9], 'rs',  label='line 2')

如果两种方法指定了相同的指标,那么默认取第一种方式。

  • 第三种方式:通过Line2D对象line的set_*方法设置
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.arange(0, 5, 0.1)
>>> line, = plt.plot(x, x*x) # plot返回一个列表,通过line,获取其第一个元素
>>> # 调用Line2D对象的set_*方法设置属性值
>>> line.set_antialiased(False)

The kwargs are Line2D properties:

PropertyDescription
agg_filtera filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
alphafloat (0.0 transparent through 1.0 opaque)
animatedbool
antialiased or aa[True | False]
clip_boxa Bbox instance
clip_onbool
clip_path[(Path, Transform) | Patch | None]
color or cany matplotlib color
containsa callable function
dash_capstyle[‘butt’ | ‘round’ | ‘projecting’]
dash_joinstyle[‘miter’ | ‘round’ | ‘bevel’]
dashessequence of on/off ink in points
drawstyle[‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’]
figurea Figure instance
fillstyle[‘full’ | ‘left’ | ‘right’ | ‘bottom’ | ‘top’ | ‘none’]
gidan id string
labelobject
linestyle or ls[‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) | '-' | '--' | '-.' | ':' | 'None' | ' ' | '']
linewidth or lwfloat value in points
markerA valid marker style
markeredgecolor or mecany matplotlib color
markeredgewidth or mewfloat value in points
markerfacecolor or mfcany matplotlib color
markerfacecoloralt or mfcaltany matplotlib color
markersize or msfloat
markevery[None | int | length-2 tuple of int | slice | list/array of int | float | length-2 tuple of float]
path_effectsAbstractPathEffect
pickerfloat distance in points or callable pick function fn(artist, event)
pickradiusfloat distance in points
rasterizedbool or None
sketch_params(scale: float, length: float, randomness: float)
snapbool or None
solid_capstyle[‘butt’ | ‘round’ | ‘projecting’]
solid_joinstyle[‘miter’ | ‘round’ | ‘bevel’]
transforma matplotlib.transforms.Transform instance
urla url string
visiblebool
xdata1D array
ydata1D array
zorderfloat

同样,可以通过调用Line2D对象的get_*方法获取对象的属性值。

>>> line.get_linewidth()
1.0
  • 第四种方式:plt.setp()函数配置多个Line2D对象的颜色和线宽属性
>>> # 同时绘制sin和cos两条曲线,lines是一个有两个Line2D对象的列表
>>> lines = plt.plot(x, np.sin(x), x, np.cos(x)) #
>>> # 调用setp函数同时配置多个Line2D对象的多个属性值
>>> plt.setp(lines, color="r", linewidth=2.0)
同样,我们可以通过plt.getp()函数获取对象的属性。
>>> plt.getp(lines[0], "color") # 返回color属性
'r'
>>> plt.getp(lines[1]) # 输出全部属性
alpha = 1.0
animated = False
antialiased or aa = True
axes = Axes(0.125,0.1;0.775x0.8)
... ...

总结:figure对象是最下面的“画纸”,plot是画,Line2D是画的属性对象,制定其属性可以通过plot也可以通过Line2D。

另外:

matplotlib的整个图表为一个Figure对象,此对象在调用plt.figure函数时返回,我们也可以通过plt.gcf函数获取当前的绘图对象:

>>> f = plt.gcf()
>>> plt.getp(f)
alpha = 1.0
animated = False
...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值