Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!
Plotly's python package is updated frequently. Run pip install plotly --upgrade
to use the latest version.
import plotly
plotly.__version__
'3.1.0'
By default, plotly.iplot()
and plotly.plot()
create public graphs (which are free to create). With a plotly subscription you can easily make charts private or secret via the sharing argument.
import plotly.plotly as py
import plotly.graph_objs as go
data = [
go.Scatter(
x=[1, 2, 3],
y=[1, 3, 1]
)
]
py.iplot(data, filename='privacy-public', sharing='public')
Below is the URL of this public plot. Anyone can view public plots even if they are not logged into Plotly. Go ahead and try it out:
py.plot(data, filename='privacy-public', sharing='public')
'https://plot.ly/~jordanpeterson/1083'
py.iplot(data, filename='privacy-private', sharing='private')
Below is the URL of the private plot above. Only the owner can view the private plot. You won't be able to view this plot, try it out:
py.plot(data, filename='privacy-private', sharing='private')
'https://plot.ly/~jordanpeterson/1085'
py.iplot(data, filename='privacy-secret', sharing='secret')
Below is the URL of this secret plot. Anyone with the secret link can view this chart. However, it will not appear in the Plotly feed, your profile, or search engines. Go ahead and try it out:
py.plot(data, filename='privacy-secret', sharing='secret')
'https://plot.ly/~jordanpeterson/1087?share_key=mId9Rao9B6Pyh7UrNdPotP'
To make all future plots private, you can update your configuration file to create private plots by default:
import plotly
plotly.tools.set_config_file(world_readable=False, sharing='private')
This example uses Plotly's REST API
import json
import requests
from requests.auth import HTTPBasicAuth
Define variables, including YOUR USERNAME and API KEY
username = 'private_plotly' # Replace with YOUR USERNAME
api_key = 'k0yy0ztssk' # Replace with YOUR API KEY
auth = HTTPBasicAuth(username, api_key)
headers = {'Plotly-Client-Platform': 'python'}
page_size = 500
Collect filenames of ALL of your plots and
update world_readable
of each plot with a PATCH request
def get_pages(username, page_size):
url = 'https://api.plot.ly/v2/folders/all?user='+username+'&filetype=plot&page_size='+str(page_size)
response = requests.get(url, auth=auth, headers=headers)
if response.status_code != 200:
return
page = json.loads(response.content.decode('utf-8'))
yield page
while True:
resource = page['children']['next']
if not resource:
break
response = requests.get(resource, auth=auth, headers=headers)
if response.status_code != 200:
break
page = json.loads(response.content.decode('utf-8'))
yield page
def make_all_plots_private(username, page_size=500):
for page in get_pages(username, page_size):
for x in range(0, len(page['children']['results'])):
fid = page['children']['results'][x]['fid']
requests.patch('https://api.plot.ly/v2/files/'+fid, {"world_readable": False}, auth=auth, headers=headers)
print('ALL of your plots are now private - visit: https://plot.ly/organize/home to view your private plots!')
make_all_plots_private(username)
ALL of your plots are now private - visit: https://plot.ly/organize/home to view your private plots!
help(py.plot)
Help on function plot in module plotly.plotly.plotly: plot(figure_or_data, validate=True, **plot_options) Create a unique url for this plot in Plotly and optionally open url. plot_options keyword arguments: filename (string) -- the name that will be associated with this figure fileopt ('new' | 'overwrite' | 'extend' | 'append') -- 'new' creates a 'new': create a new, unique url for this plot 'overwrite': overwrite the file associated with `filename` with this 'extend': add additional numbers (data) to existing traces 'append': add additional traces to existing data lists auto_open (default=True) -- Toggle browser options True: open this plot in a new browser tab False: do not open plot in the browser, but do return the unique url sharing ('public' | 'private' | 'secret') -- Toggle who can view this graph - 'public': Anyone can view this graph. It will appear in your profile and can appear in search engines. You do not need to be logged in to Plotly to view this chart. - 'private': Only you can view this plot. It will not appear in the Plotly feed, your profile, or search engines. You must be logged in to Plotly to view this graph. You can privately share this graph with other Plotly users in your online Plotly account and they will need to be logged in to view this plot. - 'secret': Anyone with this secret link can view this chart. It will not appear in the Plotly feed, your profile, or search engines. If it is embedded inside a webpage or an IPython notebook, anybody who is viewing that page will be able to view the graph. You do not need to be logged in to view this plot. world_readable (default=True) -- Deprecated: use "sharing". Make this figure private/public
from IPython.display import display, HTML
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
'privacy.ipynb', 'python/privacy/', 'Privacy',
'How to set the privacy settings of plotly graphs in python. Three examples of different privacy options: public, private and secret.',
title = 'Privacy | plotly',
name = 'Privacy', language='python',
has_thumbnail= True, thumbnail= 'thumbnail/privacy.jpg',
display_as='chart_studio', order=2,
ipynb= '~notebook_demo/97')
Collecting git+https://github.com/plotly/publisher.git Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-req-build-oEmPsN Building wheels for collected packages: publisher Running setup.py bdist_wheel for publisher ... done Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-NzZ88C/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966 Successfully built publisher Installing collected packages: publisher Found existing installation: publisher 0.11 Uninstalling publisher-0.11: Successfully uninstalled publisher-0.11 Successfully installed publisher-0.11
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead. /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you "Save" this notebook before running this command? Remember to save, always save.