Python数据可视化-matplotlib学习1
2018-03-03 10:44
926 查看
介绍:
matplotlib是Python著名的绘图库,提供了一整套和matlib相似的命令API,适合交互式制图,或者,将它作为绘图控件嵌入到GUI应用程序中。
pyplot子库:
该子库提供了类似matlib的绘图API,可以方便的绘制2D图表。
使用过程:
1.导入pyplot子库
plt.figure(figsize=(8,4))
3.调用plot函数进行绘图
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:
The following color abbreviations are supported:
另一种方式是提供关键字参数,即
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_*方法设置
同样,可以通过调用Line2D对象的get_*方法获取对象的属性值。>>> line.get_linewidth()
1.0第四种方式:plt.setp()函数配置多个Line2D对象的颜色和线宽属性
总结:figure对象是最下面的“画纸”,plot是画,Line2D是画的属性对象,制定其属性可以通过plot也可以通过Line2D。
另外:
matplotlib的整个图表为一个Figure对象,此对象在调用plt.figure函数时返回,我们也可以通过plt.gcf函数获取当前的绘图对象:
matplotlib是Python著名的绘图库,提供了一整套和matlib相似的命令API,适合交互式制图,或者,将它作为绘图控件嵌入到GUI应用程序中。
pyplot子库:
该子库提供了类似matlib的绘图API,可以方便的绘制2D图表。
使用过程:
1.导入pyplot子库
import matplotlib.pyplot as plt2.创建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. |
---|
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:
character | description |
---|---|
'-' | 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 |
character | color |
---|---|
‘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
Line2Dproperties:
Property | Description |
---|---|
agg_filter | a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array |
alpha | float (0.0 transparent through 1.0 opaque) |
animated | bool |
antialiasedor aa | [True | False] |
clip_box | a Bboxinstance |
clip_on | bool |
clip_path | [(Path, Transform) | Patch| None] |
coloror c | any matplotlib color |
contains | a callable function |
dash_capstyle | [‘butt’ | ‘round’ | ‘projecting’] |
dash_joinstyle | [‘miter’ | ‘round’ | ‘bevel’] |
dashes | sequence of on/off ink in points |
drawstyle | [‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’] |
figure | a Figureinstance |
fillstyle | [‘full’ | ‘left’ | ‘right’ | ‘bottom’ | ‘top’ | ‘none’] |
gid | an id string |
label | object |
linestyleor ls | [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) | '-'| '--'| '-.'| ':'| 'None'| ' '| ''] |
linewidthor lw | float value in points |
marker | A valid marker style |
markeredgecoloror mec | any matplotlib color |
markeredgewidthor mew | float value in points |
markerfacecoloror mfc | any matplotlib color |
markerfacecoloraltor mfcalt | any matplotlib color |
markersizeor ms | float |
markevery | [None | int | length-2 tuple of int | slice | list/array of int | float | length-2 tuple of float] |
path_effects | AbstractPathEffect |
picker | float distance in points or callable pick function fn(artist, event) |
pickradius | float distance in points |
rasterized | bool or None |
sketch_params | (scale: float, length: float, randomness: float) |
snap | bool or None |
solid_capstyle | [‘butt’ | ‘round’ | ‘projecting’] |
solid_joinstyle | [‘miter’ | ‘round’ | ‘bevel’] |
transform | a matplotlib.transforms.Transforminstance |
url | a url string |
visible | bool |
xdata | 1D array |
ydata | 1D array |
zorder | float |
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 ...
相关文章推荐
- 读书笔记--python数据可视化--001_matplotlib学习
- python 数据可视化 matplotlib学习三:生成随机漫步数据并使用matplotlib呈现
- python 数据可视化 matplotlib学习二 : 对折线图使用颜色映射
- python 数据可视化 matplotlib学习一:绘制简单的折线图
- Python可视化学习(2):Matplotlib快速绘图基础
- python数据挖掘课程 十一.Pandas、Matplotlib结合SQL语句可视化分析
- 基于Python的数据可视化 matplotlib seaborn pandas
- python数据分析之数据可视化matplotlib
- 压力测试监控数据通过python matplotlib进行可视化
- Python数据可视化之Matplotlib基础
- Python库(1):matplotlib数据可视化知识点
- python数据可视化matplotlib的使用
- Python数据可视化Matplotlib——Figure画布背景设置
- python中数据分析数据可视化作图matplotlib
- python数据分析学习笔记-Numpy-Matplotlib-Pandas
- Python数据可视化:Matplotlib 直方图、箱线图、条形图、热图、折线图、散点图。。。
- Python数据可视化图像库MatPlotLib基本图像操作
- Python进阶(三十八)-数据可视化の利用matplotlib 进行折线图,直方图和饼图的绘制
- Python可视化学习(1):Matplotlib的配置
- [置顶] Python数据可视化—matplotlib笔记