数据科学入门与实战:Matplotlib绘图基础一

为什么用python画图

  • GUI太复杂
  • Excel太头疼
  • python简单免费

什么是matplotlib
一个Python包用于2D绘图
还有很多扩展如:seaborn

# hello world in matplotlib
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
#魔法函数..
x = np.linspace(0,2 * np.pi ,100)
y = np.sin(x)
plt.plot(x,y)

在这里插入图片描述

matplotlib架构

  • Backend:主要处理吧图显示到哪里和画到哪里?
  • Artist:图像显示成什么样
  • 最上层,Scripting:pyplot,python语法和API

推荐网站 matplotlib.org 官方网站
一个简单的例子

a = [1,2,3]
b = [4,5,6]
plt.plot(a)#可以生成可视化的图

在这里插入图片描述

plt.plot(a,b)

看看坐标轴变化
在这里插入图片描述
改变线的形式

plt.plot(a,b,'*')#线的形式
plt.plot(a,b,'--')#线的形式

在这里插入图片描述

plt.plot(a,b,'r--')#线的形式

在这里插入图片描述
可以同时在一幅图作图

c = [10,8,6]
d = [1,8,3]
plt.plot(a,b,'r--',c,d,'b*')

在这里插入图片描述
画曲线图

t = np.arange(0,2,0.1)
print(t.size)
print(t)
s = np.sin(t*np.pi)
print(s)

在这里插入图片描述
有关设置

plt.plot(t,s,'r--',label = 'aaaa')
#设置x y label
plt.xlabel('this is x')
plt.ylabel('this is y')
plt.title(' this is a demo')
plt.legend()#样例

在这里插入图片描述

plt.plot(t,s,'r--',label = 'aaaa')
plt.plot(t*2,s,'b*',label = 'bbbb')
#设置x y label
plt.xlabel('this is x')
plt.ylabel('this is y')
plt.title(' this is a demo')
plt.legend()#样例

在这里插入图片描述
subplot画子图

x = np.linspace(0,5)
y1 = np.sin(np.pi * x)
y2 = np.cos(np.pi * x * 2)
#subplot画子图
plt.subplot(2,1,1)#最后一个1代表2*1第一个子图
plt.plot(x,y1,'b--',label = 'sin(pi * x)')
plt.plot(x,y2,'r--',label = 'cos(pi * pi * 2 ')
plt.xlabel('x value')
plt.ylabel('y value')
plt.legend()
plt.title('this is x y value')

在这里插入图片描述
继续使用subplot

plt.subplot(2,2,1)
plt.plot(x,y1,'b--')
plt.ylabel('y1')
plt.xlabel('x')
plt.subplot(2,2,2)
plt.plot(x,y2,'r--')
plt.ylabel('y2')
plt.xlabel('x')

plt.subplot(2,2,3)

plt.subplot(2,2,4)

在这里插入图片描述
还可以这样使用

figure,ax = plt.subplots(2,2)
ax[0][0].plot(x,y1)
ax[0][1].plot(x,y2)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值