最近又用到了matplotlib 中画图的函数。总结几个常用的函数的作用于区别。

from matplotlib import pyplot as plt

1.figure()

函数定义matplotlib.pyplot.figure(num=Nonefigsize=Nonedpi=Nonefacecolor=Noneedgecolor=Noneframeon=TrueFigureClass=<class 'matplotlib.figure.Figure'>clear=False**kwargs)

plt.figure()创建一个画布。

主要讲一个参数num:相当于给画布定义一个id,如果给出了num,之前没有使用,则创建一个新的画布;如果之前使用了这个num,那么返回那个画布的引用,在之前的画布上继续作图。如果没有给出num, 则每次创建一块新的画布。

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import interp1dx=np.linspace(0,10*np.pi,num=20)
y=np.sin(x)
yn=np.cos(x)
f1=interp1d(x,y,kind='linear')#线性插值
f2=interp1d(x,y,kind='cubic')#三次样条插值
x_pred=np.linspace(0,10*np.pi,num=1000)
y1=f1(x_pred)
y2=f2(x_pred)
plt.figure(1)
plt.plot(x_pred,y1,'r',label='linear')
plt.plot(x_pred,y2,'b--',label='cubic')
plt.legend()
# plt.show()
plt.figure(2)
plt.plot(x,yn,label='new')
plt.legend()
plt.show()

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import interp1dx=np.linspace(0,10*np.pi,num=20)
y=np.sin(x)
yn=np.cos(x)
f1=interp1d(x,y,kind='linear')#线性插值
f2=interp1d(x,y,kind='cubic')#三次样条插值
x_pred=np.linspace(0,10*np.pi,num=1000)
y1=f1(x_pred)
y2=f2(x_pred)
plt.figure(1)
plt.plot(x_pred,y1,'r',label='linear')
plt.plot(x_pred,y2,'b--',label='cubic')
plt.legend()
# plt.show()
plt.figure(1)
plt.plot(x,yn,label='new')
plt.legend()
plt.show()

2 add_subplot()

add_subplot(*args**kwargs)

向图中加入子图的轴。返回子图的坐标轴axes

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import interp1dx=np.linspace(0,10*np.pi,num=20)
y=np.sin(x)
yn=np.cos(x)
f1=interp1d(x,y,kind='linear')#线性插值
f2=interp1d(x,y,kind='cubic')#三次样条插值
x_pred=np.linspace(0,10*np.pi,num=1000)
y1=f1(x_pred)
y2=f2(x_pred)
fig = plt.figure()
fig.add_subplot(221)
plt.plot(x_pred,y1,'r',label='linear')
fig.add_subplot(222)
plt.plot(x_pred,y2,'b--',label='cubic')plt.show()

通过ax设置各种图的参数。

import matplotlib.pyplot as pltfig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.text(3, 8, 'boxed italics text in data coords', style='italic',bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)
# ax.text(3, 2, unicode('unicode: Institut f\374r Festk\366rperphysik', 'latin-1'))
ax.text(0.95, 0.01, 'colored text in axes coords',verticalalignment='bottom', horizontalalignment='right',transform=ax.transAxes,color='green', fontsize=15)
ax.plot([2,3,4], [1,2,5], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),arrowprops=dict(facecolor='black', shrink=0.05))
ax.axis([0, 10, 0, 11])
plt.show()

3.subplot()

matplotlib.pyplot.subplot(*args**kwargs):当前图中加子图

plt.subplot(221)# equivalent but more general
ax1=plt.subplot(2, 2, 1)
import numpy as np
import matplotlib.pyplot as plt# Fixing random state for reproducibility
np.random.seed(19680801)x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)plt.subplot(321)
plt.scatter(x, y, s=80, c=z, marker=">")plt.subplot(322)
plt.scatter(x, y, s=80, c=z, marker=(5, 0))verts = np.array([[-1, -1], [1, -1], [1, 1], [-1, -1]])
plt.subplot(323)
plt.scatter(x, y, s=80, c=z, marker=verts)plt.subplot(324)
plt.scatter(x, y, s=80, c=z, marker=(5, 1))plt.subplot(325)
plt.scatter(x, y, s=80, c=z, marker='+')plt.subplot(326)
plt.scatter(x, y, s=80, c=z, marker=(5, 2))plt.show()

4. subplots()

matplotlib.pyplot.subplots(nrows=1ncols=1sharex=Falsesharey=Falsesqueeze=Truesubplot_kw=Nonegridspec_kw=None**fig_kw) :创建一个图形和一组子图。

nrows, ncols : int, optional, default: 1 子图网络的行列数。.

examples:

#First create some toy data:
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)#Creates just a figure and only one subplot
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')#Creates two subplots and unpacks the output array immediately
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)#Creates four polar axes, and accesses them through the returned array
fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
axes[0, 0].plot(x, y)
axes[1, 1].scatter(x, y

返回: fig, ax

import matplotlib.pyplot as pltdata = {'apples': 10, 'oranges': 15, 'lemons': 5, 'limes': 20}
names = list(data.keys())
values = list(data.values())fig, axs = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
axs[0].bar(names, values)
axs[1].scatter(names, values)
axs[2].plot(names, values)
fig.suptitle('Categorical Plotting')

个人喜好用1画一个图,4画多个图。

class matplotlib.axes.Axes(figrectfacecolor=Noneframeon=Truesharex=Nonesharey=Nonelabel=''xscale=Noneyscale=None, **kwargs):

The Axes contains most of the figure elements: AxisTickLine2DTextPolygon, etc., and sets the coordinate system.

python matplotlib:figure,add_subplot,subplot,subplots讲解实现相关推荐

  1. python matplotlib.figure.Figure.add_subplot()方法的使用

    官方文档 https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=add_subplot#matplotl ...

  2. Python matplotlib数据可视化 subplot绘制多个子图

    数据可视化的时候,有时需要将多个子图放在同一个画板上进行比较.通过使用GridSpec类配合subplot,可以很容易对子区域进行划定和选择,在同一个画板上绘制多个子图. 原文链接:https://y ...

  3. python中subplot是什么意思_python matplotlib中的subplot函数使用详解

    python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包.基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数.于是,为了节省时间,可以一劳 ...

  4. python中mat函数_python matplotlib中的subplot函数使用详解

    python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包.基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数.于是,为了节省时间,可以一劳 ...

  5. pythonsubplot_python matplotlib中的subplot函数使用详解

    python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包.基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数.于是,为了节省时间,可以一劳 ...

  6. python matplotlib fig = plt.figure() fig.add_subplot()

    一.matplotlib.pyplot.figure() Create a new figure, or activate an existing figure. matplotlib官网 功能: 创 ...

  7. python matplotlib在一张画布上画多个图的两种方法,plt.subplot(),plt.subplots()。

    Matplotlib在一张画布上画多个图的两种方法,plt.subplot,plt.subplots. 目录 回顾 plt.subplots()画法 plt.subplot()画法 保存 回顾 之前也 ...

  8. 【python matplotlib 】fig, ax = plt.subplots()画多表图

    文章目录 一. fig, ax = plt.subplots()的作用 二.参数的含义 三.图上排列多个子图 四.把多个子图一起合并到一个图上 五.画图刻度.图例等字体.字体大小.刻度密度.线条样式设 ...

  9. pycharm matplotlib.pyplot.figure().add_subplot()绘制三维图时报错:ValueError: Unknown projection 3d(bug)

    报错描述 出于安全考虑,CSDN不让文章标题使用英文单引号 ValueError: Unknown projection '3d' # -*- coding: utf-8 -*- "&quo ...

最新文章

  1. python TypeError: ‘module‘ object is not callable
  2. Pytorch实践中的几个重要概念
  3. python多线程 不在main_Python多线程
  4. 我的世界——用一桶水一直灭岩浆一直刷黑曜石
  5. php limit限流,php+redis 限流
  6. css固定表格表头(各浏览器通用)
  7. Fikker反向代理服务器的网站缓存加速/网站加速基础教程
  8. python time,datetime当前时间,昨天时间,时间戳和字符串的转化
  9. 【多视图几何】TUM 课程 第2章 刚体运动
  10. 生日快乐模板php,可会有人跟我说句生日快乐
  11. CentOS6.4x64_安装Qt5
  12. html轮播图原理,30_用js实现一个轮播图效果,简单说下原理
  13. CPython对象模型:基础
  14. uni-app app开发对接网易云信IM
  15. 产品经理培训还好找工作吗?
  16. 约翰霍普金斯大学计算机专业,美国约翰霍普金斯大学计算机科学专业有哪些介绍...
  17. 2017 ICPCECIC 北方邀请赛 H MJF wants to work (贪心)
  18. 为什么Android系统比ios系统卡?
  19. (转)视觉工程师笔试知识汇总
  20. php半透明,php水印代码,php半透明水印支持png透明背景

热门文章

  1. 绑定到对象上的copyWithin方法
  2. java格式化输出双精度小数,用Java格式化双精度类型
  3. win7安装mysql8.0创建用户_CentOS如何安装MySQL8.0、创建用户并授权的详细步骤
  4. TypeError: Cannot read property ‘range‘ of null
  5. iosetup mysql_InnoDB: Error: io_setup() failed with EAGAIN after 5 attempt
  6. vmware配置centos7网络
  7. 关于angular2更新时机的一些发现
  8. JSONP解决跨域及ajax同步问题
  9. python中类的方法里面变量前加self与不加self的区别
  10. 一起学并发编程 - 钩子函数(shutdownHook)