<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Overview of Plotly for Python
Victoria Gregory
4/1/2016
What is Plotly?
plotly.js
Step1:  Getting started
Easy to install
Step2:  The following code will make a simple line and scatter plot
Step3:  Figure objects store data like a Python dictionary.
Step4:  Can save a static image as well
Step5:  Histograms
Step6:  Distplots
Similar to seaborn.distplot. Plot a histogram, kernel density or normal curve, and a rug plot all together.
Step7:  2D Contour Plot
Step8:  3D Surface Plot
Plot the function
Step9:  Matplotlib Conversion
<ASSISTANT_TASK:>
Python Code:
import plotly.tools as tls
tls.embed('https://plot.ly/~AnnaG/1/nfl-defensive-player-size-2013-season/')
tls.embed('https://plot.ly/~chris/7378/relative-number-of-311-complaints-by-city/')
tls.embed('https://plot.ly/~empet/2922/a-scoreboard-for-republican-candidates-as-of-august-17-2015-annotated-heatmap/')
tls.embed('https://plot.ly/~vgregory757/2/_2014-us-city-populations-click-legend-to-toggle-traces/')
Explanation: Overview of Plotly for Python
Victoria Gregory
4/1/2016
What is Plotly?
plotly.js: online JavaScript graphing library
Today I'll talk about its Python client
Both plotly.js and the Python library are free and open-source
Similar libraries for Julia, R, and Matlab
What can I do with Plotly?
Useful for data visualization and fully interactive graphics
Standard graphics interface across languages
Easily shareable online
20 types of charts, including statistical plots, 3D charts, and maps
Complete list here
Just a few examples...
End of explanation
# (*) 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
Explanation: Getting started
Easy to install: pip install plotly
How to save and view files?
Can work offline and save as .html files to open on web browser
Jupyter notebook
Upload to online account for easy sharing: import statement automatically signs you in
How It Works
Graph objects
Same structure as native Python dictionaries and lists
Defined as new classes
Every Plotly plot type has its own graph object, i.e., Scatter, Bar, Histogram
All information in a Plotly plot is contained in a Figure object, which contains
a Data object: stores data and style options, i.e., setting the line color
a Layout object: for aesthetic features outside the plotting area, i.e., setting the title
trace: refers to a set of data meant to be plotted as a whole (like an $x$ and $y$ pairing)
Interactivity is automatic!
Line/Scatter Plots
The following import statements load the three main modules:
End of explanation
# 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
)
# (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'
)
# (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) 
print(fig)  # print the figure object in notebook
Explanation: The following code will make a simple line and scatter plot:
End of explanation
# (5) Send Figure object to Plotly and show plot in notebook
py.iplot(fig, filename='scatter-mode')
Explanation: Figure objects store data like a Python dictionary.
End of explanation
py.image.save_as(fig, filename='scatter-mode.png')
Explanation: Can save a static image as well:
End of explanation
# (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
)
# (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
)
# (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)
# (5) Send Figure object to Plotly and show plot in notebook
py.iplot(fig, filename='histogram_example')
Explanation: Histograms
End of explanation
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)
# Plot!
py.iplot(fig, filename='Distplot with Multiple Datasets', \
         validate=False)
Explanation: Distplots
Similar to seaborn.distplot. Plot a histogram, kernel density or normal curve, and a rug plot all together.
End of explanation
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))]) 
Explanation: 2D Contour Plot
End of explanation
# 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)
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
    )
)
# Make a figure object
fig = go.Figure(data=data, layout=layout)
# (@) Send to Plotly and show in notebook
py.iplot(fig, filename='surface')
Explanation: 3D Surface Plot
Plot the function: $f(x,y) = A \cos(\pi x y) e^{-(x^2+y^2)/2}$
End of explanation
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()
py.iplot_mpl(fig)
Explanation: Matplotlib Conversion
End of explanation
<END_TASK>