This notebook shows you how to set up everything you need in order to start using Plotly Entreprise and make plots from Plotly's Python API.
For more examples and information about Plotly's Python API, refer to our online documentation page or our User Guide.
Go to etest.plot.ly, click on the Sign up button and fill in your info:
Note that users that already have a free plot.ly account must sign up again (with the same or with a different username) on the Plotly Entreprise server.
Then, click on the Settings tab in the drop-down menu in the top-right corner of the page:
Your API key and username are listed under Profile. Hold on to them, you will need them in step 4.
The easiest way to install Plotly's Python API is via the package manager pip. If you do not have pip installed on your machine, follow pip's installation guidelines.
Then, run inside a terminal (on Mac OS or Linux) or command prompt (on Windows):
$ pip install plotly
without the leading $
, which simply indicates that the command must be ran inside a terminal/command prompt. Note that, on Mac OS or Linux machines,
$ sudo pip install plotly
results in a system-wide installation and
$ pip install --user plotly
results in user-only installation.
If you already have Plotly's Python package installed, please check its version.
Inside Python or IPython, run
import plotly
plotly.__version__
'1.0.26'
To follow the instructions in step 4, version 1.0.26 or up of Plotly's Python is required.
Upgrade by running, inside a terminal/command prompt:
$ pip install plotly --upgrade
Or, on Mac OS and Linux machines, with a leading sudo
or --user
after install
for system-wide or user-only upgrades respectively.
Now, to make Plotly's Python API know about the Plotly Entreprise server, run in Python or IPython:
import plotly.tools as tls
tls.set_config_file('http://etest.plot.ly')
Not that, by default, plotly calls are directed to http://plot.ly
.
Next, import the plotly.plotly
module, where all the functions that allow Python to communicate with Plotly resides, and sign in using your username and API key of step 2:
import plotly.plotly as py
username='etienne-entreprise' # fill in with your username
api_key='hyfrgd51ly' # and your api-key!
py.sign_in(username,api_key)
You are now ready to start making plots with Plotly Entreprise and Plotly's Python API.
Plotly plots are built from the ground up using graph objects. For more information on graph object, refer to Plotly's Python API User Guide.
For now, import the graph objects needed to make this Plotly figure:
# embed Plotly plot in notebook,
# with username ('etienne-entreprise') and plot id ('3')
tls.embed('etienne-entreprise','3')
from plotly.graph_objs import Figure, Data, Layout
from plotly.graph_objs import Histogram, Scatter
Next, make a simply dataset consisting of an arrray of normally distributed random numbers and a gaussian curve.
import numpy as np
X = np.random.randn(100) # 100 normally distributed random numbers
xx = np.linspace(-4,4,200)
gaussian = 1./np.sqrt(2*np.pi)*np.exp(-xx**2/2) # a gaussian curve (stand. dev. =1)
Next, build a data object with two traces:
# Histogram object
my_histogram = Histogram(
x= X, # array of samples points
histnorm='probability density' # choose a normalization
)
#
my_scatter = Scatter(
x= xx, # x coordinates
y= gaussian, # y coordinates
fill='tozeroy' # fill from line down to y=0
)
# Package traces into data object
my_data = Data([my_histogram, my_scatter])
On to the layout object:
my_layout = Layout(
title='Plotly Entreprise welcomes you', # title of the plot
showlegend=False, # remove legend
)
And finally, wrap all graph objects up into the figure object:
my_fig = Figure(data=my_data, layout=my_layout)
We are now ready to call Plotly and get our plot. There are two ways to do this
Use the py.plot()
function to send a figure object to Plotly, get the plot's URL in return and show it in a new tab in your web browser.
Or, if using an IPython notebook, use the py.iplot()
function which sends a figure object to Plotly and gets in return the Plotly plot embedded inside the IPython notebook cell below.
As we are working inside an IPython notebook here, let's use py.iplot()
:
py.iplot(my_fig, filename='plotly-enterprise')
Hover, zoom in and out or pan the figure directly inside this IPython notebook.
To go to the graph's URL, simply click on data and graph on the bottom-right corner of the cell.
About Plotly
Big thanks to
# CSS styling within IPython notebook
from IPython.core.display import HTML
def css_styling():
styles = open("../../python-user-guide/custom.css", "r").read()
return HTML(styles)
css_styling()