(Matplotlib)画图基础与简易线形图

1.画图基础
1.1导入文件包

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
#设置绘图样式
plt.style.use('classic')

1.2三种显示图形方式

# (1)启动Matplotlib模式, 在notebook中启动交互式图形
#%matplotlib notebook

# (2)启动Matplotlib模式, 在notebook中启动静态图形
%matplotlib inline

fig = plt.figure()
x = np.linspace(0,10,100)
plt.plot(x, np.sin(x),'-')
plt.plot(x, np.cos(x),'--')

#将图形保存为文件(必须放在plt.show()前)
plt.savefig('my_figure.png')

#(3)在脚本中画图
#plt.show()

在这里插入图片描述
1.3显示Image对象

from IPython.display import Image
Image('my_figure.png')

在这里插入图片描述
1.4查看系统支持的文件格式

fig.canvas.get_supported_filetypes()

在这里插入图片描述
2.两种画图接口
2.1MATLAB风格接口(pyplot)

#创建图形
plt.figure()
#创建两个子图中的第一个,设置坐标轴
plt.subplot(2,1,1) #(行,列,子图编号)
plt.plot(x, np.sin(x))

#创建两个子图中的第二个,设置坐标轴
plt.subplot(2,1,2)
plt.plot(x, np.cos(x))

在这里插入图片描述
2.2面向对象接口(Axes)

#先创建图形网格
#ax是包含两个Axes对象的数组
fig, ax = plt.subplots(2)

#在每个对象上调用plot()方法
ax[0].plot(x, np.sin(x))
ax[1].plot(x, np.cos(x))

在这里插入图片描述
3.简易线形图

plt.style.use('seaborn-whitegrid')
#图形实例
fig = plt.figure()
#坐标轴实例
ax = plt.axes()

x = np.linspace(0,10,100)
ax.plot(x, np.sin(x))
ax.plot(x, np.cos(x))

在这里插入图片描述

#使用pylab接口
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

在这里插入图片描述
3.1调整图形:颜色与风格

plt.plot(x, np.sin(x - 0), color='blue')       # 标准颜色名称
plt.plot(x, np.sin(x - 1), color='g')          # 缩写颜色代码(rgbcmyk)
plt.plot(x, np.sin(x - 2), color='0.75')       # 范围在0-1的灰度值
plt.plot(x, np.sin(x - 3), color='#FFDD44')    # 十六进制(RRGGBB, 00-FF)
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3))# RGB元组,范围在0-1
plt.plot(x, np.sin(x - 5), color='chartreuse') # HTML颜色名称

在这里插入图片描述

plt.plot(x, x+0, linestyle='-')   #实线
plt.plot(x, x+1, linestyle='--')  #虚线
plt.plot(x, x+2, linestyle='-.')  #点划线
plt.plot(x, x+3, linestyle=':')   #实点线

在这里插入图片描述

#将linestyle,color组合使用
plt.plot(x, x+0, '-g')   #绿色实线
plt.plot(x, x+1, '--c')  #青色虚线
plt.plot(x, x+2, '-.k')  #黑色点划线
plt.plot(x, x+3, ':r')   #红色实点线

在这里插入图片描述
3.2调整图形:坐标轴上下限

# 使用plt.xlim(), plt.ylim()
plt.plot(x, np.sin(x))
plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5)

在这里插入图片描述

#坐标轴逆序显示
plt.plot(x, np.sin(x))
plt.xlim(10, 0)
plt.ylim(1.2, -1.2)

在这里插入图片描述

# 使用plt.axis()设置坐标轴
plt.plot(x, np.sin(x))
plt.axis([-1, 11, -1.5, 1.5]) #[xmin, xmax, ymin, ymax]

在这里插入图片描述

plt.plot(x, np.sin(x))
plt.axis('tight') #收紧

在这里插入图片描述

plt.plot(x, np.sin(x))
plt.axis('equal') #等单位长度

在这里插入图片描述
3.3设置图形标签

plt.plot(x, np.sin(x))
plt.title('A Sine Curve')
plt.xlabel('x')
plt.ylabel('sin(x)')

在这里插入图片描述

# 显示图例
plt.plot(x, np.sin(x), '-g', label='sin(x)')
plt.plot(x, np.cos(x), ':b', label='cos(x)')
plt.axis('equal')
plt.legend()

在这里插入图片描述
4.Matplotlib陷阱
虽然绝大多数的 plt 函数都可以直接转换成 ax 方法(例如 plt.plot() → ax.plot()、 plt.legend() → ax.legend() 等),但是并非所有的命令都可以这样用。尤其是用来设 置坐标轴上下限、坐标轴标题和图形标题的函数,它们大都稍有差别。一些 MATLAB 风格的方法和面向对象方法的转换如下所示:
• plt.xlabel() → ax.set_xlabel()
• plt.ylabel() → ax.set_ylabel()
• plt.xlim() → ax.set_xlim()
• plt.ylim() → ax.set_ylim()
• plt.title() → ax.set_title()
在用面向对象接口画图时,不需要单独调用这些函数,通常采用 ax.set() 方法一次性 设置所有的属性是更简便的方法:

ax = plt.axes()
ax.plot(x, np.sin(x))
ax.set(xlim=(0,10), ylim=(-2,2),
      xlabel='x', ylabel='sin(x)',
      title='A Simple Plot')

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值