文章目录
- 1 matplot的基本用法
- 1.1 导入模块:import matplotlib.pyplot as plt
- 1.2 定义图像窗口:plt.figure()
- 1.3 画图:plt.plot(x, y)
- 1.4 定义坐标轴范围:plt.xlim()/plt.ylim()
- 1.5 定义坐标轴名称:plt.xlabel()/plt.ylabel()
- 1.6 定义坐标轴刻度及名称:plt.xticks()/plt.yticks()
- 1.7 设置图像边框颜色:ax = plt.gca() ax.spines[].set_color()
- 1.8 调整刻度位置:ax.xaxis.set_ticks_position()/ax.yaxis.set_ticks_position()
- 1.9 调整边框(坐标轴)位置:ax.spines[].set_position()
- 2 图例与标注
1 matplot的基本用法
1.1 导入模块:import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
1.2 定义图像窗口:plt.figure()
plt.figure(num = 1, figsize = (8, 5)) #编号为1,窗口大小为(8, 5)
运行结果:
1.3 画图:plt.plot(x, y)
x = np.linspace(-5, 5, 50)
y1 = x*2 + 2
y2 = x** + 1
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '--') #linestyle可选参数为'-', '--', '-.', ':'
plt.show()
运行结果:
1.4 定义坐标轴范围:plt.xlim()/plt.ylim()
x = np.linspace(-5, 5, 50)
y1 = x*2 + 2
y2 = x**2 + 1
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '--') #linestyle可选参数为'-', '--', '-.', ':'
plt.plot(x, y2, color = 'blue', linewidth = 0.5, linestyle = '-.')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.show()
运行结果:
1.5 定义坐标轴名称:plt.xlabel()/plt.ylabel()
x = np.linspace(-5, 5, 50)
y1 = x*2 + 2
y2 = x**2 + 1
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '--') #linestyle可选参数为'-', '--', '-.', ':'
plt.plot(x, y2, color = 'blue', linewidth = 0.5, linestyle = '-.')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xlabel('i am x')
plt.ylabel('i am y')
plt.show()
运行结果:
1.6 定义坐标轴刻度及名称:plt.xticks()/plt.yticks()
x = np.linspace(-5, 5, 50)
y1 = x*2 + 2
y2 = x**2 + 1
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '--') #linestyle可选参数为'-', '--', '-.', ':'
plt.plot(x, y2, color = 'blue', linewidth = 0.5, linestyle = '-.')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xlabel('i am x')
plt.ylabel('i am y')
plt.yticks([-0.5, 1, 1.5], ['bad', 'normal', 'good'])
plt.show()
运行结果:
1.7 设置图像边框颜色:ax = plt.gca() ax.spines[].set_color()
x = np.linspace(-5, 5, 50)
y1 = x*2 + 2
y2 = x**2 + 1
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '--') #linestyle可选参数为'-', '--', '-.', ':'
plt.plot(x, y2, color = 'blue', linewidth = 0.5, linestyle = '-.')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xlabel('i am x')
plt.ylabel('i am y')
plt.yticks([-0.5, 1, 1.5], ['bad', 'normal', 'good'])
ax = plt.gca()
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
plt.show()
运行结果:
1.8 调整刻度位置:ax.xaxis.set_ticks_position()/ax.yaxis.set_ticks_position()
x = np.linspace(-5, 5, 50)
y1 = x*2 + 2
y2 = x**2 + 1
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '--') #linestyle可选参数为'-', '--', '-.', ':'
plt.plot(x, y2, color = 'blue', linewidth = 0.5, linestyle = '-.')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xlabel('i am x')
plt.ylabel('i am y')
plt.yticks([-0.5, 1, 1.5], ['bad', 'normal', 'good'])
ax = plt.gca()
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.show()
运行结果:
1.9 调整边框(坐标轴)位置:ax.spines[].set_position()
x = np.linspace(-5, 5, 50)
y1 = x*2 + 2
y2 = x**2 + 1
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '--') #linestyle可选参数为'-', '--', '-.', ':'
plt.plot(x, y2, color = 'blue', linewidth = 0.5, linestyle = '-.')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xlabel('i am x')
plt.ylabel('i am y')
plt.yticks([-0.5, 1, 1.5], ['bad', 'normal', 'good'])
ax = plt.gca()
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.show()
运行结果:
2 图例与标注
2.1 添加图例:plt.legend()
x = np.linspace(-10, 10, 50)
y1 = x*2 + 2
y2 = x**3
plt.figure(num = 1, figsize = (8, 5))
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '-', label = 'linear line')
plt.plot(x, y2, color = 'blue', linewidth = 1, linestyle = '--', label = 'square line')
plt.xlim(-5, 5)
plt.ylim(-10, 10)
plt.legend(loc = 'best')
运行结果:
2.2 画点:plt.scatter()
x = np.linspace(-10, 10, 50)
y1 = x*2 + 2
y2 = x**3
plt.figure(num = 1, figsize = (8, 5))
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '-', label = 'linear line')
plt.plot(x, y2, color = 'blue', linewidth = 1, linestyle = '--', label = 'square line')
plt.xlim(-5, 5)
plt.ylim(-10, 10)
ax = plt.gca()
ax.spines['top'].set_color('w')
ax.spines['right'].set_color('w')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
x0 = 1
y0 = x0*2 + 2
plt.plot([x0,x0], [0,y0], color = 'k')
plt.scatter([x0, ], [y0, ], s = 50, color = 'b')
plt.legend(loc = 'best')
运行结果:
2.3 添加标注:plt.annotate()
x = np.linspace(-10, 10, 50)
y1 = x*2 + 2
y2 = x**3
plt.figure(num = 1, figsize = (8, 5))
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '-', label = 'linear line')
plt.plot(x, y2, color = 'blue', linewidth = 1, linestyle = '--', label = 'square line')
plt.xlim(-5, 5)
plt.ylim(-10, 10)
ax = plt.gca()
ax.spines['top'].set_color('w')
ax.spines['right'].set_color('w')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
x0 = 1
y0 = x0*2 + 2
plt.plot([x0,x0], [0,y0], color = 'k')
plt.scatter([x0, ], [y0, ], s = 50, color = 'b')
plt.annotate('2x+2 = %s' % y0, xy = (x0, y0), xycoords = 'data', xytext = (+30, -30), textcoords = 'offset points', fontsize = 16)
plt.legend(loc = 'best')
运行结果:
2.4 添加注释:plt.text()
x = np.linspace(-10, 10, 50)
y1 = x*2 + 2
y2 = x**3
plt.figure(num = 1, figsize = (8, 5))
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '-', label = 'linear line')
plt.plot(x, y2, color = 'blue', linewidth = 1, linestyle = '--', label = 'square line')
plt.xlim(-5, 5)
plt.ylim(-10, 10)
ax = plt.gca()
ax.spines['top'].set_color('w')
ax.spines['right'].set_color('w')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
x0 = 1
y0 = x0*2 + 2
plt.plot([x0,x0], [0,y0], color = 'k')
plt.scatter([x0, ], [y0, ], s = 50, color = 'b')
plt.annotate('2x+2 = %s' % y0, xy = (x0, y0), xycoords = 'data', xytext = (+30, -30), textcoords = 'offset points', fontsize = 16)
plt.text(-4-1, 5-2, 'this is the some text.', fontdict = {'size': 16, 'color':'r'})
plt.legend(loc = 'best')
运行结果:
2.5 添加注释 text
x = np.linspace(-10, 10, 50)
y1 = x*2 + 2
y2 = x**3
plt.figure(num = 1, figsize = (8, 5))
plt.plot(x, y1, color = 'red', linewidth = 1, linestyle = '-', label = 'linear line')
plt.plot(x, y2, color = 'blue', linewidth = 1, linestyle = '--', label = 'square line')
plt.xlim(-5, 5)
plt.ylim(-10, 10)
ax = plt.gca()
ax.spines['top'].set_color('w')
ax.spines['right'].set_color('w')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
x0 = 1
y0 = x0*2 + 2
plt.plot([x0,x0], [0,y0], color = 'k')
plt.scatter([x0, ], [y0, ], s = 50, color = 'b')
plt.annotate('2x+2 = %s' % y0, xy = (x0, y0), xycoords = 'data', xytext = (+30, -30), textcoords = 'offset points', fontsize = 16)
plt.text(-4-1, 5-2, 'this is the some text.', fontdict = {'size': 16, 'color':'r'})
plt.legend(loc = 'best')
运行结果:
运行结果:
运行结果:
运行结果: