In [13]:
import plotly.tools as tls
tls.embed('https://plot.ly/~AnnaG/1/nfl-defensive-player-size-2013-season/')
Out[13]:
In [3]:
tls.embed('https://plot.ly/~chris/7378/relative-number-of-311-complaints-by-city/')
Out[3]:
In [5]:
tls.embed('https://plot.ly/~empet/2922/a-scoreboard-for-republican-candidates-as-of-august-17-2015-annotated-heatmap/')
Out[5]:
In [6]:
tls.embed('https://plot.ly/~vgregory757/2/_2014-us-city-populations-click-legend-to-toggle-traces/')
Out[6]:
Figure object, which containsData object: stores data and style options, i.e., setting the line colorLayout object: for aesthetic features outside the plotting area, i.e., setting the title
In [1]:
# (*) Tools to communicate with Plotly's server
import plotly.plotly as py
# (*) Useful Python/Plotly tools
import plotly.tools as tls
# (*) Graph objects to piece together your Plotly plots
import plotly.graph_objs as go
The following code will make a simple line and scatter plot:
In [2]:
# Create random data with numpy
import numpy as np
N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5
# (1.1) Make a 1st Scatter object
trace0 = go.Scatter(
x = random_x,
y = random_y0,
mode = 'markers',
name = '$\mu = 5$',
hoverinfo='x+y' # choosing what to show on hover
)
In [3]:
# (1.2) Make a 2nd Scatter object
trace1 = go.Scatter(
x = random_x,
y = random_y1,
mode = 'lines+markers',
name = '$\mu = 0$',
hoverinfo='x+y'
)
# (1.3) Make a 3rd Scatter object
trace2 = go.Scatter(
x = random_x,
y = random_y2,
mode = 'lines',
name = '$\mu = -5$',
hoverinfo='x+y'
)
In [4]:
# (2) Make Data object
# Data is list-like, must use [ ]
data = go.Data([trace0, trace1, trace2])
# (3) Make Layout object (Layout is dict-like)
layout = go.Layout(title='$\\text{Some scatter objects distributed as } \
\mathcal{N}(\mu,1)$',
xaxis=dict(title='x-axis label'),
yaxis=dict(title='y-axis label'),
showlegend=True)
# (4) Make Figure object (Figure is dict-like)
fig = go.Figure(data=data, layout=layout)
In [5]:
print(fig) # print the figure object in notebook
Figure objects store data like a Python dictionary.
In [6]:
# (5) Send Figure object to Plotly and show plot in notebook
py.iplot(fig, filename='scatter-mode')
Out[6]:
Can save a static image as well:
In [59]:
py.image.save_as(fig, filename='scatter-mode.png')
In [ ]:
# (1) Generate some random numbers
x0 = np.random.randn(500)
x1 = np.random.randn(500)+1
# (2.1) Create the first Histogram object
trace1 = go.Histogram(
x=x0,
histnorm='count',
name='control',
autobinx=False,
xbins=dict(
start=-3.2,
end=2.8,
size=0.2
),
marker=dict(
color='fuchsia',
line=dict(
color='grey',
width=0
)
),
opacity=0.75
)
In [7]:
# (2.2) Create the second Histogram object
trace2 = go.Histogram(
x=x1,
name='experimental',
autobinx=False,
xbins=dict(
start=-1.8,
end=4.2,
size=0.2
),
marker=dict(
color='rgb(255, 217, 102)'
),
opacity=0.75
)
In [8]:
# (3) Create Data object
data = [trace1, trace2]
# (4) Create Layout object
layout = go.Layout(
title='Sampled Results',
xaxis=dict(
title='Value'
),
yaxis=dict(
title='Count'
),
barmode='overlay',
bargap=0.25,
bargroupgap=0.3,
showlegend=True
)
fig = go.Figure(data=data, layout=layout)
In [9]:
# (5) Send Figure object to Plotly and show plot in notebook
py.iplot(fig, filename='histogram_example')
Out[9]:
In [ ]:
from plotly.tools import FigureFactory as FF
# Add histogram data
x1 = np.random.randn(200)-2
x2 = np.random.randn(200)
x3 = np.random.randn(200)+2
x4 = np.random.randn(200)+4
# Group data together
hist_data = [x1, x2, x3, x4]
group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
# Create distplot with custom bin_size
fig = FF.create_distplot(hist_data, group_labels, bin_size=.2)
In [63]:
# Plot!
py.iplot(fig, filename='Distplot with Multiple Datasets', \
validate=False)
Out[63]:
In [3]:
x = np.random.randn(1000)
y = np.random.randn(1000)
py.iplot([go.Histogram2dContour(x=x, y=y, \
contours=go.Contours(coloring='fill')), \
go.Scatter(x=x, y=y, mode='markers', \
marker=go.Marker(color='white', size=3, opacity=0.3))])
Out[3]:
In [5]:
# Define the function to be plotted
def fxy(x, y):
A = 1 # choose a maximum amplitude
return A*(np.cos(np.pi*x*y))**2 * np.exp(-(x**2+y**2)/2.)
# Choose length of square domain, make row and column vectors
L = 4
x = y = np.arange(-L/2., L/2., 0.1) # use a mesh spacing of 0.1
yt = y[:, np.newaxis] # (!) make column vector
# Get surface coordinates!
z = fxy(x, yt)
In [6]:
trace1 = go.Surface(
z=z, # link the fxy 2d numpy array
x=x, # link 1d numpy array of x coords
y=y # link 1d numpy array of y coords
)
# Package the trace dictionary into a data object
data = go.Data([trace1])
# Dictionary of style options for all axes
axis = dict(
showbackground=True, # (!) show axis background
backgroundcolor="rgb(204, 204, 204)", # set background color to grey
gridcolor="rgb(255, 255, 255)", # set grid line color
zerolinecolor="rgb(255, 255, 255)", # set zero grid line color
)
# Make a layout object
layout = go.Layout(
title='$f(x,y) = A \cos(\pi x y) e^{-(x^2+y^2)/2}$', # set plot title
scene=go.Scene( # (!) axes are part of a 'scene' in 3d plots
xaxis=go.XAxis(axis), # set x-axis style
yaxis=go.YAxis(axis), # set y-axis style
zaxis=go.ZAxis(axis) # set z-axis style
)
)
In [8]:
# Make a figure object
fig = go.Figure(data=data, layout=layout)
# (@) Send to Plotly and show in notebook
py.iplot(fig, filename='surface')
Out[8]:
In [ ]:
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
n = 50
x, y, z, s, ew = np.random.rand(5, n)
c, ec = np.random.rand(2, n, 4)
area_scale, width_scale = 500, 5
fig, ax = plt.subplots()
sc = ax.scatter(x, y, c=c,
s=np.square(s)*area_scale,
edgecolor=ec,
linewidth=ew*width_scale)
ax.grid()
In [6]:
py.iplot_mpl(fig)
Out[6]: