PLT的基础用法

如代码:

import matplotlib.pyplot as plt
import numpy as np

#准备绘图数据
x = np.arange(0,1,0.05)
y = np.sin(2*np.pi*x)
y2 = np.cos(2*np.pi*x)
plt.plot(x,y)
plt.show()

'''
#改变颜色
plt.plot(x,y,'r')
plt.show()

#变成虚线
plt.plot(x,y,'r--')
plt.show()

#用特定符号显示数据点
plt.plot(x,y,'r--*',label = 'sin') #label用于添加图示例
#加标题
plt.title("My First Plot")
#给横坐标、纵坐标加标签
plt.xlabel("x label")
plt.ylabel("y label")
#设置图像位置
plt.legend(loc = 'best')
plt.show()
'''

'''
#figure 和 subplot 绘制多个图表
fig = plt.figure()
ax1 = fig.add_subplot(221)  #221表示创建一个两行两列子图,并且这是第一个图
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
#将上面的曲线放到ax2中
ax2.plot(x,y)
plt.show()
'''

#上面的创建方法也可以简化为
fig,ax = plt.subplots(2,2)
ax[0,1].plot(x,y,'g--*',label = 'sin')
ax[0,1].set(title = 'My First Plot',xlabel = 'x',ylabel = 'y')
ax[0,1].legend(loc = 'best')
#背景上添加网格线
ax[0,1].grid()

ax[0,0].plot(x,y2,'r*',label = 'cos')
ax[0,0].set(title = 'cos plot',xlabel = 'x',ylabel = 'y')
ax[0,0].legend(loc = 'best')
ax[0,0].grid()
plt.show()

#将正弦曲线和余弦曲线绘在一张图上
fign,axn = plt.subplots()
axn.plot(x,y,'g--*',label = 'sin')
axn.plot(x,y2,'r^',label = 'cos')
axn.set(title = 'combine',xlabel = 'x',ylabel = 'y')
axn.grid()
axn.legend('best')
plt.show()

#将图表保存到本地
fign.savefig('myfig.png')



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值