matplotlib-1学习

1
2
3
import matplotlib.pyplot as plt
print(plt.plot([1,3,2,4]))
plt.show()

第1句,这是导入Matplotlib接口的主要子模块pyplot绘图的首选格式。这是最好的做法,也是为了避免对全局名称空间的污染,强烈鼓励永远不要像这样导入接口:

1
from <module> import *

第2句,此代码行是实际的绘图命令。我们只指定了一个值列表,这些值表示要绘制的点的垂直坐标。matplotlib将使用一个隐式的水平值列表,从0(前值)到n-1(n为列表中的条目数),纵轴表示Y轴,横坐标表示X轴。

第3句,这条命令实际上是打开包含绘图图像的窗口。

1
2
3
x = range(6)
plt.plot(x, [xi**2 for xi in x])
plt.show()

1.多线图

如果需要在一个图中画出多条线,我们只需要在show之前多次plot就可以了。Matplotlib会给每条线自动选择不同颜色。

1
2
3
4
5
x = range(1, 5)
plt.plot(x, [xi*1.5 for xi in x])
plt.plot(x, [xi*3.0 for xi in x])
plt.plot(x, [xi/3.0 for xi in x])
plt.show()

也可以写成下列形式:

1
plt.plot(x, [xi*1.5 for xi in x], x, [xi*3.0 for xi in x], x,[xi/3.0 for xi in x])

或者

1
plt.plot(x, x*1.5, x, x*3.0, x, x/3.0)

2、网格

1
plt.grid(True)

3、坐标轴

你可能已经注意到,matplotlib为了精确地包含绘制的数据集自动设置图的界限。有时我们想自己设置坐标轴的界限。我们可以这样:

1
2
3
4
5
6
7
8
9
import matplotlib.py
plot as plt
import numpy as npx = np.arange(1, 5)
plt.plot(x, x*1.5, x, x*3.0, x, x/3.0)
print(plt.axis()) # shows the current axis limits values,
# (0.85, 4.15, -0.25000000000000006, 12.583333333333334)
# plt.axis([0, 5, -1, 13]) # set new axes limits
# [0, 5, -1, 13]
plt.show()

如果axis()函数没有参数,它的返回值是实际的界限,有两种方式给这个函数传,一是通过四个值得列表[xmin, xmax, ymin, ymax] ,二是键值对的方式。

我们也可以只限制某个轴的最大值或者最小值

1
plt.axis(xmin=NNN, ymax=NNN)

或者通过xlim()和ylim()函数只限制某一个坐标轴的界限。

4、给坐标增加一个标签

1
2
3
4
5
import matplotlib.pyplot as plt
plt.plot([1, 3, 2, 4])
plt.xlabel('This is the X axis')
plt.ylabel('This is the Y axis')
plt.show()

5、增加一个标题

1
plt.title('Simple plot')

6、增加图例

1
2
3
4
plt.plot(x, x*1.5, label='Normal')
plt.plot(x, x*3.0, label='Fast')
plt.plot(x, x/3.0, label='Slow')
plt.legend()

7、保存图到文件

1
2
plt.savefig('plot123.png')
plt.savefig('plot123.png', dpi=200)

8、Plot()支持可选的第三个参数

1
plt.plot(X, Y, '<format>', ...)

plot方法的关键字参数color(或c)用来设置线的颜色。

控制颜色

1
2
3
4
plt.plot(y, 'y')
plt.plot(y+1, 'm')
plt.plot(y+2, 'c')
plt.show()

1)颜色的缩写

Color abbreviation Color Name
b blue
c cyan
g green
k black
m magenta
r red
w white
y yellow 2

2)十六进制字符 #FF00FF

3)(r, g, b) 或 (r, g, b, a),其中 r g b a 取均为[0, 1]之间

4)[0, 1]之间的浮点数的字符串形式,表示灰度值。0表示黑色,1表示白色

也可写成

1
2
plt.plot(x1, y1, fmt1, x2, y2, fmt2, ...)
plt.plot(y, 'y', y+1, 'm', y+2, 'c')

9、控制线条样式

1
plt.plot(y, '--', y+1, '-.', y+2, ':')

- solid

– dashed

-. dashdot

: dotted

‘’, ‘ ‘, None

10、控制标记样式

Marker abbreviation Marker style
. Point marker
, Pixel marker
o Circle marker
v Triangle down marker
^ Triangle up marker
< Triangle left marker
> Triangle right marker
1 Tripod down marker
2 Tripod up marker
3 Tripod left marker
4 Tripod right marker
s Square marker
p Pentagon marker
* Star marker
h Hexagon marker
H Rotated hexagon marker
+ Plus marker
x Cross (x) marker
D Diamond marker
d Thin diamond marker
\ Vertical line (vline symbol) marker
_ Horizontal line (hline symbol) marker
1
2
3
4
5
import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3, 0.3)
plt.plot(y, 'cx--', y+1, 'mo:', y+2, 'kp-.');
plt.show()

传递关键字参数

1
2
3
4
5
6
7
import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3, 0.3)
plt.plot(y, color='blue', linestyle='dashdot', linewidth=4,
marker='o', markerfacecolor='red', markeredgecolor='black',
markeredgewidth=3, markersize=12);
plt.show()

如果想画两条一样颜色的线,可以这样:

1
plt.plot(x1, y1, x2, y2, color='green')

11、X轴和Y轴的刻度

1
locs, labels = plt.xticks()

不加任何参数的画,tick函数返回当前位置和标签,

1
2
print(plt.xticks())
#(array([0. , 0.2, 0.4, 0.6, 0.8, 1. ]), <a list of 6 Text xticklabel objects>)
1
2
3
4
5
6
import matplotlib.pyplot as plt 
x = [5, 3, 7, 2, 4, 1]
plt.plot(x);
plt.xticks(range(len(x)), ['a', 'b', 'c', 'd', 'e', 'f']);
plt.yticks(range(1, 8, 2));
plt.show()

使用字符来代表x的刻度

12、中文字体不显示的问题

1
2
3
4
from pylab import mpl

mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

参考:Matplotlib for python development

python中matplotlib的颜色及线条控制:http://blog.csdn.net/qq_26376175/article/details/67637151

本文标题:matplotlib-1学习

文章作者:goingcoder

发布时间:2018年02月02日 - 22:02

最后更新:2018年02月02日 - 22:02

原始链接:https://goingcoder.github.io/2018/02/02/matplotlib1/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------