大家好,又见面了,我是你们的朋友全栈君。
安装visdom
pip install visdom
打开服务
python -m visdom.server
浏览器中打开对应的网址。
首先创建一个新的环境,如不创建则默认在main环境下
#_*_coding:utf-8 _*_
import numpy as np
import visdom
import time
viz = visdom.Visdom(env="Test1") # 创建环境名为Test1
下面代码在一个image窗口中不断更新显示图像
#单张图像显示与更新demo
image = viz.image(np.random.rand(3,256,256),opts={'title':'image1','caption':'How random.'})
for i in range(10):
viz.image(np.random.randn( 3, 256, 256),win = image)
time.sleep(0.5)
#多图像显示与更新demo
images = viz.images(
np.random.randn(20, 3, 64, 64),
opts=dict(title='Random images', caption='How random.',nrow=5)
)
for i in range(10):
viz.images(np.random.randn(20, 3, 64, 64),win = images)
time.sleep(0.5)
#text 显示与更新显示demo,将在窗口中连续打印字符
text = viz.text('Hello World !')
strForOut = "This is a string for you to print!"
out = ""
for i in range(len(strForOut)):
out = out + strForOut[i]
viz.text(out,win = text)
time.sleep(0.2)
#绘画折线图演示
x=0
name=['acc','loss','loss2']
for i in range(50):
y = np.random.randint(5, size=(1, 3))
viz.line(Y=y,X=np.ones(y.shape)*x,
win='line',
opts=dict(legend=name,
title='line test',
width=800,
height=800,
xlabel='Time',
ylabel='Volume'),
update=None if x == 0 else 'append'
)
time.sleep(0.1)
x+=1
# 绘制散点图演示 二维
colors = np.random.randint(0, 255, (3, 3,))#第一维3表示该数据可以分为三类,以三种颜色的三点来比表示
win = viz.scatter(
X=np.random.rand(255, 2),#表示要展示的散点数据
#Y=(np.random.rand(255) + 1.5).astype(int),
Y=np.random.randint(1,4,(255)),#每一个数据的类别,将以其对应的colors中的颜色来显示
opts=dict(
markersize=5,
markercolor=colors,
legend=['1', '2','3'],
markersymbol = 'cross-thin-open'
),
)
# 绘制散点图演示 三维
colors = np.random.randint(0, 255, (3, 3,))#第一维3表示该数据可以分为三类,以三种颜色的三点来比表示
win = viz.scatter(
X=np.random.rand(255, 3),#表示要展示的散点数据
#Y=(np.random.rand(255) + 1.5).astype(int),
Y=np.random.randint(1,4,(255)),#每一个数据的类别,将以其对应的colors中的颜色来显示
opts=dict(
markersize=5,
markercolor=colors,
legend=['1', '2','3'],
markersymbol = 'cross-thin-open'
),
)
#实时更新绘制散点图
legend=['1', '2','3']
Scatter = viz.scatter(
X=np.array([[0,0]]),
Y=np.array([1]),
opts=dict(
markersize=5,
legend=legend,
# markersymbol = 'cross-thin-open'
),
)
for i in range(20):
X = np.random.rand(1,2)
Y = np.random.randint(1,4,1)
print(Y)
viz.scatter(
X=X,
Y=Y,
win=Scatter,
update= 'append',
name = legend[Y[0]-1],
opts=dict(
markersize=5,
# markersymbol = 'cross-thin-open'
)
)
time.sleep(0.5)
更多应用请参考官方demo: https://github.com/facebookresearch/visdom/blob/master/example/demo.py
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149107.html原文链接:https://javaforall.cn