python 利用matplotlib中imshow()函数绘图

matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab类似的命令API,十分适合交互式地进行制图。并且也能够方便地将它做为绘图控件,嵌入GUI应用程序中。经过简单的绘图语句,就能够绘制出高质量的图了。python

这里咱们就主要讲一下inshow()函数的使用。ide

首先看一下怎么基本画图的流程:函数

import matplotlib.pyplot as plt 

#建立新的figure
fig = plt.figure()

#必须经过add_subplot()建立一个或多个绘图
ax = fig.add_subplot(221)

#绘制2x2两行两列共四个图,编号从1开始
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

#图片的显示
plt.show()

截图以下所示:
在这里插入图片描述
热图(heatmap)是数据分析的经常使用方法,经过色差、亮度来展现数据的差别、易于理解。Python在Matplotlib库中,调用imshow()函数实现热图绘制。学习

'''
遇到问题没人解答?小编建立了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#coding=utf-8
import matplotlib.pyplot as plt 
import numpy as np

points = np.arange(-5,5,0.01)

xs,ys = np.meshgrid(points,points)

z = np.sqrt(xs**2 + ys**2)

#建立新的figure
fig = plt.figure()

#绘制2x2两行两列共四个图,编号从1开始
ax = fig.add_subplot(221)
ax.imshow(z)

ax = fig.add_subplot(222)
#使用自定义的colormap(灰度图)
ax.imshow(z,cmap=plt.cm.gray)

ax = fig.add_subplot(223)
#使用自定义的colormap
ax.imshow(z,cmap=plt.cm.cool)

ax = fig.add_subplot(224)
#使用自定义的colormap
ax.imshow(z,cmap=plt.cm.hot)

#图片的显示
plt.show()

输出结果:
在这里插入图片描述
错误备忘:code

问题一: NameError: name 'imshow' is not definedorm

解决方案:在文件中添加,视频

from pylab import *

问题二:ImportError: No module named _internalblog

解决方案:缘由是安装好pip 后续又安装了pip,致使版本冲突。教程

解决方法:
后续又安装了pip,致使版本冲突。图片

解决方法:

sudo apt remove python-pip
相关文章
相关标签/搜索