绘图工具(一):visdom,matplotlib.pyplot,PLT 之visdom

1.visdom

visdom是Facebook专门为Pytorch开发的一款可视化工具。在 pytorch可视化之visdom使用中已经对如何使用visdom做了简要介绍。
下面主要从代码方面介绍下。

(1)先看下代码和结果:


import time
from visdom import Visdom
import requests
import os
import numpy as np
import torch

viz = Visdom(server='http://127.0.0.1', port=8097)
assert viz.check_connection()

# video
video_file = 'demo.ogv'
if not os.path.exists(video_file):
    video_url = 'http://media.w3.org/2010/05/dintel/trailer.ogv'
    res = requests.get(video_url)
    with open(video_file, 'wb') as f:
        f.write(res.content)

##---------picture-------------##
# single picture
viz.image(
    np.random.rand(3, 512, 256),
    opts = {
        'title': 'Random',
    }
)

# multiple picture
viz.images(
    #np.random.rand(20, 3, 50, 50),
    #torch.randn(3,500,500),
    torch.randn(4,3,300,300),
    opts = {
        'title':'tulti-images',
    }
)

# scatter point
Y = np.random.rand(100)
Y = (Y[Y>0] + 1.5).astype(int)

old_scatter = viz.scatter(

    X=np.random.rand(100, 2)*100,
    Y=Y,
    opts={
        'title': 'Scatter',
        'legend': ['A', 'B'],
        'xtickmin': 0,
        'xtickstep': 10,
        'ytickmax': 100,
        'ytickstep': 10,
        'matkersymbolk': 'cross-thin-open',
        'width': 800,
        'height': 600
    },
)

# time.sleep(5)
# 更新样式

viz.update_window_opts(
    win=old_scatter,
    opts={
        'tittle': 'New Scatter',
        'legend': ['Apple', 'Banana'],
        'markersymbol': 'dot'

    }
)

# 3D scatter map
viz.scatter(
    X=np.random.rand(100, 3),
    Y=Y,
    opts={
        'title': '3D Scatter',
        'legend': ['Men', 'Women'],
        'makersize': 2
    }
)

# histogram
viz.bar(X=np.random.rand(20))
viz.bar(
    X=np.abs(np.random.rand(5,3)), # five column, every column has three component
    opts={
        'stacked': True,
        'lengend': ['A', 'B', 'C'],
        'rownames': ['2012', '2013', '2014', '2015', '2016']
    }
)

# heatmap
viz.heatmap(
    X=np.outer(np.arange(1,6), np.arange(1,11)),
    opts={
        'columnnames': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
        'rownames': ['y1', 'y2', 'y3', 'y4', 'y5'],
        'colormap': 'Electric'
    }
)

# surface
x = np.tile(np.arange(1,101), (100,1))
y = x.transpose()
X = np.exp((((x - 50) ** 2) + ((y - 50) ** 2)) / -(20.**2))
viz.contour(X=X, opts=dict(colormap='Viridis'))

#
viz.surf(X=X, opts={'colormap': 'Hot'})
17074337-4ff36170f39c50f1.png
chrome截图.png

(2)绘图命令

单张图片:(image)

viz = Visdom(server='http://127.0.0.1', port=8097)
# 可以接受ndarray数据和torch.tensor 数据
# image 只能绘制一个图片
viz.image(                             
 np.random.rand(3, 512, 256),          # 输入的图片矩阵(二维或者三维),可以是[1, h, w](灰度图),或者[3, h, w](rgb图), 或者[h, w](和[1, h, w]相同)
    opts = {                                      
        'title': 'Random',
)

多张图片:(images)

# images可以绘制多张图片
viz.images(
# 输入的矩阵为[batch, 1或3,  h,  w](batch张图片,1 为二值图像,3为彩色图像),也可以[1或3,h,  w](显示一张图片),[h, w]显示一张二值图片。
    #np.random.rand(20, 3, 50, 50),
    #torch.randn(3,500,500),
    torch.randn(4,3,300,300),
    opts = {
        'title':'tulti-images',
    }
)

散点图:(scatter)

Y = np.random.rand(100)   # 01之间均匀分布
Y = (Y[Y>0] + 1.5).astype(int)  # 取整到1或者2

old_scatter = viz.scatter(

    X=np.random.rand(100, 2)*100,   # 二维或者三维矩阵,指定每一个点坐标位置
    Y=Y,   # 指定每个点的标签
    opts={
        'title': 'Scatter',
        'legend': ['A', 'B'],
        'xtickmin': 0,
        'xtickstep': 10,
        'ytickmax': 100,
        'ytickstep': 10,
        'matkersymbolk': 'cross-thin-open',
        'width': 800,
        'height': 600
    },
)

3. python图像库PLT(Python Imaging Library )

猜你喜欢

转载自blog.csdn.net/weixin_34357267/article/details/90967703