使用matplotlib画图时有时会收到来自matplotlib的runtime warming的警告,原因可能是同时打开太多张图,最常见的情况是在一个循环中画图,每次循环都新建一个图,但是未关闭新建的图,当循环次数多了之后内存就吃不消了。
有两种解决方法,一是只建一个图,每次循环结束后通过plt.cla()清除图的内容,下次循环可以使用同一张图作画,例子如下:
import os
import scipy
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
data_path = r"D:\PycharmProjects\dataset"
def load_mnist():
path = os.path.join(data_path, 'mnist')
fd = open(os.path.join(path, 't10k-images-idx3-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
teX = loaded[16:].reshape((10000, 28, 28, 1)).astype(np.float)
teX = teX / 255.
return teX
teX = load_mnist()
fig, ax = plt.subplots(nrows=5, ncols=5, sharex='all', sharey='all') # 只建一张包含25个子图的图
ax = ax.flatten()
for j in range(3):
for i in range(25):
img = teX[i + j * 25].reshape(28, 28)
ax[i].imshow(img, cmap='Greys', interpolation='nearest')
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout() # 自动紧凑布局
plt.savefig(r"D:\test\%d.png" % j)
plt.cla() # 清除内容
第二种方法是每次循环都新建一张图,但是每次循环结束后关闭这张图,例子如下:
import os
import scipy
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
data_path = r"D:\PycharmProjects\dataset"
def load_mnist():
path = os.path.join(data_path, 'mnist')
fd = open(os.path.join(path, 't10k-images-idx3-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
teX = loaded[16:].reshape((10000, 28, 28, 1)).astype(np.float)
teX = teX / 255.
return teX
teX = load_mnist() # 获取mnist的测试数据
for j in range(3): # 画三张图
fig, ax = plt.subplots(nrows=5, ncols=5, sharex='all', sharey='all') # 每次都新建一张包含25个子图的图
ax = ax.flatten()
for i in range(25):
img = teX[i + j * 25].reshape(28, 28)
ax[i].imshow(img, cmap='Greys', interpolation='nearest')
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout() # 自动紧凑布局
plt.savefig(r"D:\test\%d.png" % j)
plt.close()
实验证明,用第二种方法会比第一种方法快很多
使用matplotlib画图时有时会收到来自matplotlib的runtime warming的警告,原因可能是同时打开太多张图,最常见的情况是在一个循环中画图,每次循环都新建一个图,但是未关闭新建的图,当循环次数多了之后内存就吃不消了。有两种解决方法,一是只建一个图,每次循环结束后通过plt.cla()清除图的内容,下次循环可以使用同一张图作画,例子如下:import osimpo...
用python的matplotlib画图时,往往需要加图例说明。如果不设置任何参数,默认是加到图像的内侧的最佳位置。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
for i in xrange(5):
ax.plot(x, i * x, label='$y = %ix$' % i)
plt.legend()
plt.show()
这样的结果如图所示:
如果需要将该legend移到图像外侧,有多种方法,这里介绍一种。
我现在有一组图片,一共100张图片,每张大小是200*200,即imgs.shape=100*200*200*3 (注意通道数在最后一维)。 我需要同时绘制这100张图片,而且每张图片需要写上对应的名字,所以这里假设你已经准备好了你的图像数据,即
imgs = [
[np.random.rand(200,200,3), '1.jpg'],
[np.ran...
Matplotlib_绘图时会同时出现两张图,其中一张没有图形
Matplotlib_绘图时同时出现两张图,其中一张没有图线*
在使用matplotlib的绘图时,我们对图形的参数进行各种自定义,但定义后,发现绘图时会同时出现两张图,如下:
它的代码如下:
解决方法:这个时候只要将plt.figure()函数移到最前面就可以了
这是因为在你定义xlabel和ylabel时,matplotlib中并没有图形,这时候matplotlib就会定义一个图形figure1去接收你传进来的xlabel和yla
首先一幅
Matplotlib的图像组成部分介绍。
在
matplotlib中,整个图像为一个Figure对象。在Figure对象中可以包含一个或者多个Axes对象。每个Axes(ax)对象都是一个拥有自己坐标系统的绘图区域。所属关系如下:
subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
Create a figure and a set of subplots
This utility ...
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>压缩图片</title>
</head>
MNIST 数据集已经是一个被”嚼烂”了的数据集, 很多教程都会对它”下手”, 几乎成为一个 “典范”. 不过有些人可能对它还不是很了解, 下面来介绍一下.MNIST 数据集可在 http://yann.lecun.com/exdb/mnist/ 获取, 它包含了四个部分:
Training set images: train-images-idx3-ubyte.gz (9.9 MB, 解压后 47
首先,需要安装
matplotlib库。在命令行中输入:pip install
matplotlib
然后,在代码中导入
matplotlib库:import
matplotlib.pyplot as plt
接着,
使用相应的函数绘制图形。例如,可以
使用plt.plot()绘制折线图,
使用plt.scatter()绘制散点图,
使用plt.bar()绘制柱状图等。
最后,
使用plt.show()命令显示图形。
import
matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()
这是一个简单的折线图。
王学强_Bryan:
PPT立方体三面同色
qq_46389354: