python数据可视化库之matplotlib

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')

运行结果:
在这里插入图片描述


运行结果:


运行结果:


运行结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值