pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法

前言

学习吴恩达老师的deeplearningai课程,完成第二周编程作业二,想自己在pycharm中重新写一遍代码,发现加载完数据集后图像不显示。

环境

win10
anaconda3
python3.8
pychrm-community-2022.1.3

解决方法

方法一:

导入pylab库(pylab是matplotlib中的一个模块,直接导入即可。)
在plt.imshow(train_set_x_orig[index])后加一句pylab.show()

import numpy as np
from matplotlib import pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
import scipy.misc
import pylab


def load_dataset():
    train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:])  # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:])  # your train set labels

    test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:])  # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:])  # your test set labels

    classes = np.array(test_dataset["list_classes"][:])  # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes


train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

index = 5
plt.imshow(train_set_x_orig[index])
pylab.show()
print("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode(
    "utf-8") + "' picture.")

在这里插入图片描述

方法二:

在plt.imshow(train_set_x_orig[index])后加一句plt.show()
在这里插入图片描述

PS

  1. 网上有中解决方法是pycharm–>File | Settings | Tools | Python Scientific | 取消勾选Show plots in tool window,但我的pycharm里根本没有Python Scientific这个选项
  2. pycharm使用plt不显示图片解决方案
    这篇博客解释了为什么在jupyter中可以显示而pycharm中不可以显示。
  3. 感觉中科大的镜像源蛮好用
pip3 install -i https://pypi.mirrors.ustc.edu.cn/simple/ pylab

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值