Skip to content

Instantly share code, notes, and snippets.

@andrewssobral
Last active November 3, 2017 12:30
Show Gist options
  • Save andrewssobral/5f1c7388eaebdd74971366f5571ce9e4 to your computer and use it in GitHub Desktop.
Save andrewssobral/5f1c7388eaebdd74971366f5571ce9e4 to your computer and use it in GitHub Desktop.
import os
import math
import numpy as np
from argparse import ArgumentParser
from visdom import Visdom
from six.moves import urllib
# General parameters
DEFAULT_VISDOM_PORT = 8097
DEFAULT_VISDOM_HOST = "127.0.0.1"
def main(args):
print("Connecting to %s" % args.port)
viz = Visdom(server="http://"+args.host,port=args.port)
assert viz.check_connection()
# text plot
textwindow = viz.text('Hello World!')
#updatetextwindow = viz.text('Hello World! More text should be here')
#viz.text('And here it is', win=updatetextwindow, append=True)
# SVG plotting
#svgstr = """
#<svg height="300" width="300">
# <ellipse cx="80" cy="80" rx="50" ry="30"
# style="fill:red;stroke:purple;stroke-width:2" />
# Sorry, your browser does not support inline SVG.
#</svg>
#"""
#viz.svg(
# svgstr=svgstr,
# opts=dict(title='Example of SVG Rendering')
#)
# boxplot
X = np.random.rand(100, 2)
X[:, 1] += 2
viz.boxplot(
X=X,
opts=dict(legend=['Men', 'Women'])
)
# stemplot
Y = np.linspace(0, 2 * math.pi, 70)
X = np.column_stack((np.sin(Y), np.cos(Y)))
viz.stem(
X=X,
Y=Y,
opts=dict(legend=['Sine', 'Cosine'])
)
# quiver plot
X = np.arange(0, 2.1, .2)
Y = np.arange(0, 2.1, .2)
X = np.broadcast_to(np.expand_dims(X, axis=1), (len(X), len(X)))
Y = np.broadcast_to(np.expand_dims(Y, axis=0), (len(Y), len(Y)))
U = np.multiply(np.cos(X), Y)
V = np.multiply(np.sin(X), Y)
viz.quiver(
X=U,
Y=V,
opts=dict(normalize=0.9),
)
# pie chart
X = np.asarray([19, 26, 55])
viz.pie(
X=X,
opts=dict(legend=['Residential', 'Non-Residential', 'Utility'])
)
# mesh plot
x = [0, 0, 1, 1, 0, 0, 1, 1]
y = [0, 1, 1, 0, 0, 1, 1, 0]
z = [0, 0, 0, 0, 1, 1, 1, 1]
X = np.c_[x, y, z]
i = [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2]
j = [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3]
k = [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6]
Y = np.c_[i, j, k]
viz.mesh(X=X, Y=Y, opts=dict(opacity=0.5))
# contour
x = np.tile(np.arange(1, 101), (100, 1))
y = x.transpose()
X = np.exp((((x - 50) ** 2) + ((y - 50) ** 2)) / -(20.0 ** 2))
viz.contour(X=X, opts=dict(colormap='Viridis'))
# surface
viz.surf(X=X, opts=dict(colormap='Hot'))
# line plots
viz.line(Y=np.random.rand(10))
Y = np.linspace(-5, 5, 100)
viz.line(
Y=np.column_stack((Y * Y, np.sqrt(Y + 5))),
X=np.column_stack((Y, Y)),
opts=dict(markers=False),
)
# heatmap
viz.heatmap(
X=np.outer(np.arange(1, 6), np.arange(1, 11)),
opts=dict(
columnnames=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
rownames=['y1', 'y2', 'y3', 'y4', 'y5'],
colormap='Electric',
)
)
# histogram
viz.histogram(X=np.random.rand(10000), opts=dict(numbins=20))
# bar plots
viz.bar(X=np.random.rand(20))
viz.bar(
X=np.abs(np.random.rand(5, 3)),
opts=dict(
stacked=True,
legend=['Facebook', 'Google', 'Twitter'],
rownames=['2012', '2013', '2014', '2015', '2016']
)
)
viz.bar(
X=np.random.rand(20, 3),
opts=dict(
stacked=False,
legend=['The Netherlands', 'France', 'United States']
)
)
# scatter plots
Y = np.random.rand(100)
viz.scatter(
X=np.random.rand(100, 2),
Y=(Y[Y > 0] + 1.5).astype(int),
opts=dict(
legend=['Apples', 'Pears'],
xtickmin=-5,
xtickmax=5,
xtickstep=0.5,
ytickmin=-5,
ytickmax=5,
ytickstep=0.5,
markersymbol='cross-thin-open',
)
)
viz.scatter(
X=np.random.rand(100, 3),
Y=(Y + 1.5).astype(int),
opts=dict(
legend=['Men', 'Women'],
markersize=5,
)
)
# image demo
#viz.image(
# np.random.rand(3, 512, 256),
# opts=dict(title='Random!', caption='How random.'),
#)
# grid of images
viz.images(
np.random.randn(20, 3, 64, 64),
opts=dict(title='Random images', caption='How random.')
)
# video demo:
try:
video = np.empty([256, 250, 250, 3], dtype=np.uint8)
for n in range(256):
video[n, :, :, :].fill(n)
viz.video(tensor=video)
# video demo: download video from http://media.w3.org/2010/05/sintel/trailer.ogv
#video_url = 'http://media.w3.org/2010/05/sintel/trailer.ogv'
#videofile = '/tmp/trailer.ogv'
# download video
#urllib.request.urlretrieve(video_url, videofile)
#if os.path.isfile(videofile):
# viz.video(videofile=videofile)
except ImportError:
print('Skipped video example')
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--host', required=False, type=str, help="visdom server host")
parser.add_argument('--port', required=False, type=str, help="visdom server port")
parser.set_defaults(host=DEFAULT_VISDOM_HOST,port=DEFAULT_VISDOM_PORT)
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment