matplotlib画图小技巧

1. import画图

import h5py
import numpy as np
import matplotlib.pyplot as plt
%pylab inline

 

2. 循环画多张图

fig,ax = plt.subplots(4,4,figsize = (20,20))
for i in range(4):
    for j in range(4):
        ax[i,j].imshow(rawA_padded[0+4*i+j],cmap=cm.Greys)

 

3. 并列画多张图

fig,ax = plt.subplots(1,2,figsize = (20,20))
ax[0].imshow(rawA_padded[0],cmap=cm.Greys)
ax[1].imshow(rawA[0],cmap=cm.Greys)

 

4. show_2d画分割结果图

def show_2d(picture, raw=None):
    m,n = picture.shape
    num = np.unique(picture)
    size = len(num)
    print("The number of nuerons is %d" % size)
    color = np.zeros([m, n, 3])
    for i in num:
        if i == 0 or i == 18446744073709551613:
            red=green=blue=0
        else:
            red = np.random.randint(255)
            green = np.random.randint(255)
            blue = np.random.randint(255)
        color[:,:,0][picture == i] = red
        color[:,:,1][picture == i] = green
        color[:,:,2][picture == i] = blue
    color = color / 255
    if raw is not None:
        plt.figure()
        plt.subplots(figsize=(10,10))
        plt.imshow(raw)
        plt.imshow(color, alpha=.8)
        plt.show()
    else:
        plt.figure()
        plt.subplots(figsize=(10,10))
        plt.imshow(color)
        plt.show()

可视化改良:

def show_2d_modified(picture, raw=None):
    m,n = picture.shape
    ids = np.unique(picture)
    size = len(ids)
    print("The number of nuerons is %d" % size)
    color = np.zeros([m, n, 3])
    idx = np.searchsorted(ids, picture)
    for i in range(3):
        color_val = np.random.randint(0, 255, ids.shape)
        if ids[0] == 0:
            color_val[0] = 0
        color[:,:,i] = color_val[idx]
    color = color / 255
    if raw is not None:
        plt.figure()
        plt.subplots(figsize=(10,10))
        plt.imshow(raw)
        plt.imshow(color, alpha=.8)
        plt.show()
    else:
        plt.figure()
        plt.subplots(figsize=(10,10))
        plt.imshow(color)
        plt.show()

3d可视化

def show_3d_modified(picture):
    d,m,n = picture.shape
    ids = np.unique(picture)
    size = len(ids)
    print("The number of nuerons is %d" % size)
    color = np.zeros([d, m, n, 3], dtype=np.uint8)
    idx = np.searchsorted(ids, picture)
    for i in range(3):
        color_val = np.random.randint(0, 255, ids.shape)
        if ids[0] == 0:
            color_val[0] = 0
        color[:,:,:,i] = color_val[idx]
    # color = color / 255
    return color

 

你可能感兴趣的:(日用小技能)