matplotlib

matplotlib

matplotlib简介

matplotlib给python提供了丰富的绘图库,尤其是二维图像绘制。

官网http://matplotlib.org/

简单介绍

Python中最著名的绘图系统,很多其他的绘图例如seaborn(针对pandas绘图而来)也是由其封装而成。创世人John Hunter于2012年离世。这个绘图系统操作起来很复杂,和R的ggplot,lattice绘图相比显得望而却步,这也是为什么我个人不丢弃R的原因,虽然调用

plt.style.use("ggplot")

绘制的图形可以大致按照ggplot的颜色显示,但是还是感觉很鸡肋。但是matplotlib的复杂给其带来了很强的定制性。其具有面向对象的方式及Pyplot的经典高层封装。

需要掌握的是:

  1. 散点图,折线图,条形图,直方图,饼状图,箱形图的绘制。

  2. 绘图的三大系统:pyplot,pylab(不推荐),面向对象

  3. 坐标轴的调整,添加文字注释,区域填充,及特殊图形patches的使用

  4. 金融的同学注意的是:可以直接调用Yahoo财经数据绘图(真。。。)

Pyplot快速入门:Pyplot tutorial

下面就是对官网pyplot快速入门教程的学习记录和翻译。

图形的基本绘制

这个已经翻译好了http://blog.csdn.net/qq_31192383/article/details/53977822

Figure_02
Figure_03
Figure_04.png

绘图设置

控制线条的属性

  • 使用关键字参数

和上一张图相比,通过对线宽属性进行设置,即linewidth = 3.0,线条明显变粗了。

Figure_05
  • 通过获得线对象,对线对象进行设置

Figure_06.png
  • dads

Figure_07

​ 同时获取多个线对象,分别使用plt.setp()函数设置每个线对象的属性。

Figure_08

Line2D线对象的属性

下面是可选的Line2D线对象的属性

Property

Value Type

alpha

float

animated

[True | False]

antialiased or aa

[True | False]

clip_box

a matplotlib.transform.Bbox instance

clip_on

[True | False]

clip_path

a Path instance and a Transform instance, a Patch

color or c

any matplotlib color

contains

the hit testing function

dash_capstyle

['butt'

dash_joinstyle

['miter'

dashes

sequence of on/off ink in points

data

(np.array xdata, np.array ydata)

figure

a matplotlib.figure.Figure instance

label

any string

linestyle or ls

[ '-'

linewidth or lw

float value in points

lod

[True | False]

marker

[ '+'

markeredgecolor or mec

any matplotlib color

markeredgewidth or mew

float value in points

markerfacecolor or mfc

any matplotlib color

markersize or ms

float

markevery

[ None | integer | (startind, stride) ]

picker

used in interactive line selection

pickradius

the line pick selection radius

solid_capstyle

['butt'

solid_joinstyle

['miter'

transform

a matplotlib.transforms.Transform instance

visible

[True | False]

xdata

np.array

ydata

np.array

zorder

any number

为了得到可以设置的线对象的属性列表,调用plt.setp()函数,该函数以线对象为参数。

Figure_09

支持的颜色

http://blog.csdn.net/mmc2015/article/details/47746195

如果颜色不显示指出,则默认循环使用不同的颜色,

character

color

‘b’

blue

‘g’

green

‘r’

red

‘c’

cyan

‘m’

magenta

‘y’

yellow

‘k’

black

‘w’

white

支持的线型

http://blog.csdn.net/mmc2015/article/details/47746195

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

绘制多个图像和轴

Figure_10
Figure_11_1
Figure_11_1
Figure_12

各种图形的绘制

柱形图

bar

两个bar并列,且加上说明和bar顶部的文字描述

bar_two

常见问题

中文乱码

具体参见如下帖子:

Mac系统彻底解决matplotlib中文显示乱码的问题

mac上Matplotlib中文乱码问题

参考资料

matplotlib简介中的简单介绍就是复制的这里。

Last updated

Was this helpful?