八种自建默认颜色缩写
b : blue
g : green
r : red
c : cyan
m : magenta
y: magenta
y : yellow
k :black
w:white
其他颜色表示方法
灰色阴影
html 十六进制
RGB元组
用数字表示,代表灰色程度有多深
y = np.arange(1, 5)
plt.plot(y+1, color='0.5') #灰色阴影
plt.plot(y+2, color='#FF00FF') #16进制
plt.plot(y+3, color=(0.1, 0.2, 0.3)) #RGB
plt.show()
查阅颜色链接:https://www.114la.com/other/rgb.htm
y = np.arange(1, 5)
plt.plot(y, marker='p')
plt.plot(y+1, marker='o')
plt.plot(y+2, marker='D')
plt.plot(y+3, marker='^')
plt.show()
注:如果写出marker会在图上加上线段, 没有加marker 图上就没有线段
y = np.arange(1, 5)
plt.plot(y, '--')
plt.plot(y+1, '-')
plt.plot(y+2, '^')
plt.plot(y+3, ':')
plt.show()
样式字符串
可以将颜色,点型,线型写成一个字符串
cx–
mo:
kp-
y = np.arange(1, 5)
plt.plot(y, 'cx--')
plt.plot(y+1, 'kp:')
plt.plot(y+2, 'mo-')
plt.plot(y+3, ':')
plt.show()
pyplot:经典高层封装,用的最多
pylab:将matplotlib 和Numpy合并的模块,模拟Matlab的编程环境
面向对象的方式:Matplotlib底层方式
pylab方式
from pylab import *
x = arange(0, 10, 1)
y = randn(len(x))
plot(x, y)
title('pylab')
show()
pyplot方式
from pylab import *
x = np.arange(0, 10, 1)
y = np.random.randn(len(x))
plt.plot(x, y)
plt.title('pyplot')
plt.show()
面向对象
x = np.arange(0, 10, 1)
y = np.random.randn(len(x))
fig = plt.figure()
ax = fig.add_subplot(111)
l, = plt.plot(x, y)
t = ax.set_title('object oriented')
plt.show()
用面向对象的方式 画出正弦函数
x = np.arange(-2*np.pi, 2*np.pi, 0.01)
y = np.sin(x)
fig = plt.figure()
ax = fig.add_subplot(111)
l, = plt.plot(x, y)
t = ax.set_title('object oriented')
plt.show()
子图方式
fig=plt.figure()
Figure实例
可以添加Axes实例
ax=fig.add_subplot(111)
返回Axes实例
参数一:子图总行数
参数二:子图总列数
参数三,子图的位置
x = np.arange(1, 100)
fig = plt.figure()
ax1 = fig.add_subplot(231)
ax1.plot(x, x)
ax2 = fig.add_subplot(232)
ax2.plot(x, -x)
plt.show()
pyplot画出图形
x = np.arange(1, 100)
plt.subplot(221)
plt.plot(x, -x)
plt.subplot(222)
plt.plot(x, np.log(x))
plt.show()
画出多个子图
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot([1, 2, 3], [3, 2, 1])
ax2 = fig1.add_subplot(111)
ax2.plot([1, 2, 3], [1, 2, 3])
plt.show()
画出网格
y = np.arange(5)
plt.plot(y, y*2)
plt.grid(True) #打开网格
plt.show()
更改网格参数
y = np.arange(5)
plt.plot(y, y*2)
plt.grid(linewidth='1', color='red', linestyle='--') #打开网格
plt.show()
面向对象的方式绘制网格
x = np.arange(0, 10, 1)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(x, x*2)
ax.grid(color='g')
plt.show()
创建图例
创建每条线的信息框
plt的方式
x = np.arange(1, 11, 1)
plt.plot(x, x*2, label='Normal')
plt.plot(x, x*3, label='Normal')
plt.plot(x, x*4, label='Normal')
plt.legend()
plt.show()
用loc参数对legend进行操作
更改图例位置
plt.legend(loc=1)
plt.legend(loc=3)
让图例扁平化一点
ncol弄成几列
plt.legend(ncol=3)
或者通过方式加上名字
x = np.arange(1, 11, 1)
plt.plot(x, x*2)
plt.plot(x, x*3)
plt.plot(x, x*4)
plt.legend(['1', '2', '3'])
plt.show()
坐标轴范围调整
x = np.arange(-10, 11, 1)
plt.plot(x, x*x)
plt.axis([-5, 5, 20, 60]) #设置x / y轴范围
plt.show()
plt.xlim([-5,5]) #只更改x轴
plt.ylim([-5,5]) #只更改y轴
调整坐标轴
x = np.arange(1, 11, 1)
plt.plot(x, x)
ax = plt.gca() #获取当前图形的坐标轴
ax.locator_params('x', nbins=5) #只调整x轴
ax.locator_params('y', nbins=5) #只调整x轴
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import datetime
import matplotlib as mpl
fig = plt.figure()
start = datetime.datetime(2015, 1, 1)
stop = datetime.datetime(2016, 1, 1)
delta = datetime.timedelta(days=1)
dates = mpl.dates.drange(start, stop, delta)
y = np.random.rand(len(dates))
ax = plt.gca()
ax.plot_date(dates, y, linestyle='-', marker='')
date_format = mpl.dates.DateFormatter('%Y-%m')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate() #根据排列大小,坐标轴自适应
plt.show()
添加新的坐标轴
使用相同的横坐标,纵坐标刻度不同
x = np.arange(2, 20, 1)
y1 = x*x
y2 = np.log(x)
plt.plot(x, y1)
plt.twinx() #添加新的坐标轴
plt.plot(x, y2, 'red')
plt.show()
面向对象的方式
x = np.arange(2, 20, 1)
y1 = x*x
y2 = np.log(x)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1)
ax1.set_ylabel('Y1')
ax2 = ax1.twinx() #添加一个新的图形
ax2.plot(x, y2, 'red')
ax2.set_ylabel('Y2')
ax1.set_xlabel('Compare Y1 and Y2')
plt.show()