matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形
折线:
plot([x], y, [fmt], data=None, **kwargs) #只有一条线
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs) #多条线
参数:fmt = '[color][marker][line]'
颜色:可以通过matplotlib.colors定义
character |
color |
'b' |
blue |
'g'
扫描二维码关注公众号,回复:
2381657 查看本文章
|
green |
'r' |
red |
'c' |
cyan |
'm' |
magenta |
'y' |
yellow |
'k' |
black |
'w' |
white |
数据点标识
character |
description |
'.' |
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 |
description |
'-' |
solid line style |
'--' |
dashed line style |
'-.' |
dash-dot line style |
':' |
dotted line style |
一条线
import matplotlib import matplotlib.pyplot x=[1,2,3,4] y=[1,2,3,5] matplotlib.pyplot.plot(x,y) matplotlib.pyplot.show()
多条线
x=[1,2,3,4] y=[1,2,3,5] x1=[10,11,12,19] y1=[3,4,5,59] matplotlib.pyplot.plot(x,y) matplotlib.pyplot.plot(x1,y1) matplotlib.pyplot.xlabel('m') matplotlib.pyplot.ylabel('rmb') matplotlib.pyplot.title('title') matplotlib.pyplot.show()
多区域
x=[1,2,3,4] y=[1,2,3,5] x1=[3,4,6,1] y1=[3,4,5,9] matplotlib.pyplot.subplot(2,1,1) #设置区域 matplotlib.pyplot.title('title1') matplotlib.pyplot.xlabel('m') matplotlib.pyplot.ylabel('rmb') matplotlib.pyplot.plot(x,y,) matplotlib.pyplot.axis([0,10,0,10])#设置坐标刻度 matplotlib.pyplot.subplot(2,1,2) matplotlib.pyplot.title('title2') matplotlib.pyplot.plot(x1,y1) matplotlib.pyplot.xlabel('m') matplotlib.pyplot.ylabel('rmb') matplotlib.pyplot.show()
多画布:
x=[1,2,3,4] y=[1,2,3,5] x1=[3,4,6,1] y1=[3,4,5,9] matplotlib.pyplot.figure(1)#画布1 matplotlib.pyplot.title('title1') matplotlib.pyplot.xlabel('m') matplotlib.pyplot.ylabel('rmb') matplotlib.pyplot.plot(x,y,) matplotlib.pyplot.axis([0,10,0,10]) matplotlib.pyplot.figure(2)#画布2 matplotlib.pyplot.title('title2') matplotlib.pyplot.plot(x1,y1) matplotlib.pyplot.xlabel('m') matplotlib.pyplot.ylabel('rmb') matplotlib.pyplot.show()
在一个画布上画多个坐标
x=[1,2,3,4] y=[1,2,3,5] x1=[0.1,0.4,0.2,0.1] y1=[0.3,0.3,0.2,0.3] fig=matplotlib.pyplot.figure(1) axe1=fig.add_axes([0.2,0.1,0.8,0.8]) #left bottom width heigh axe1.set_xlabel("x") axe1.set_ylabel("y") axe1.set_title("big") axe1.legend(loc=2) axe1.plot(x,y) axe2=fig.add_axes([0.3,0.5,0.3,0.3]) #left bottom width heigh axe2.set_xlabel("x1") axe2.set_ylabel("y1") axe2.set_title("big1") axe2.plot(x1,y1) matplotlib.pyplot.show()
多线条时注释每个线条
x=[1,2,3,4] y=[1,2,3,5] x1=[0.1,0.4,0.2,0.1] y1=[0.3,0.3,0.2,0.3] fig=matplotlib.pyplot.figure(1) axe1=fig.add_axes([0.2,0.1,0.8,0.8]) #left bottom width heigh axe1.set_xlabel("x") axe1.set_ylabel("y") axe1.set_title("big") axe1.plot(x,y,label='y=x') #设置注释 axe1.plot(x1,y1,label='y1=x1') axe1.legend(loc=2)#设置注释位置 matplotlib.pyplot.show()