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.6.1'
You can plot your graphs from a python script from command line. On executing the script, it will open a web browser with your Plotly Graph drawn.
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
plot([go.Scatter(x=[1, 2, 3], y=[3, 1, 6])])
'file:///Users/Chelsea/Repos/documentation/_posts/python/offline/temp-plot.html'
You can also plot your graphs offline inside a Jupyter Notebook Environment. First you need to initiate the Plotly Notebook mode as below:
init_notebook_mode(connected=True)
Run at the start of every ipython notebook to use plotly.offline. This injects the plotly.js source files into the notebook.
iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
import plotly.graph_objs as go
import numpy as np
x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([go.Histogram2dContour(x=x, y=y, contours=dict(coloring='heatmap')),
go.Scatter(x=x, y=y, mode='markers', marker=dict(color='white', size=3, opacity=0.3))], show_link=False)
import cufflinks as cf
iplot(cf.datagen.lines().iplot(asFigure=True,
kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns'))
All methods in plotly.plotly will communicate with a Chart Studio Cloud or Chart Studio Enterprise.
get_figure
downloads a figure from plot.ly or Chart Studio Enterprise.
You need to provide credentials to download figures: https://plotly.com/python/getting-started/
import plotly.plotly as py
fig = py.get_figure('https://plotly.com/~jackp/8715', raw=True)
iplot(fig)
The plotly.io.to_image
function can then be used to convert a plotly figure to a static image bytes string.
import plotly.io as pio
static_image_bytes = pio.to_image(fig, format='png')
Use IPython.display.Image
to display the image bytes as image in the notebook
from IPython.display import Image
Image(static_image_bytes)
Use plotly.io.write_image
to convert a figure to a static image and write it to a file or writeable object.
Make sure to add a file extension or specify the file type using the format parameter.
pio.write_image(fig, file='plotly_static_image.png', format='png')
For more information, run help(plotly.offline.iplot)
or help(plotly.offline.plot)
or help(plotly.io.to_image)
or help(plotly.io.write_image)
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(
'plotly_offline.ipynb', 'python/offline/', 'Plotly Offline for IPython Notebooks',
'How to use Plotly offline inside IPython notebooks',
title= 'Plotly Offline for IPython Notebooks',
name = 'Offline Plots in Plotly',
has_thumbnail='true',thumbnail='thumbnail/offline.png'
language='python', page_type='example_index', layout='user-guide', display_as='chart_studio'
ipynb= '~notebook_demo/267',
uses_plotly_offline=True
)
Collecting git+https://github.com/plotly/publisher.git Cloning https://github.com/plotly/publisher.git to c:\users\priyat~1\appdata\local\temp\pip-req-build-r4149umc Building wheels for collected packages: publisher Running setup.py bdist_wheel for publisher: started Running setup.py bdist_wheel for publisher: finished with status 'done' Stored in directory: C:\Users\PRIYAT~1\AppData\Local\Temp\pip-ephem-wheel-cache-9ur3vl6p\wheels\99\3e\a0\fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966 Successfully built publisher Installing collected packages: publisher Found existing installation: publisher 0.13 Uninstalling publisher-0.13: Successfully uninstalled publisher-0.13 Successfully installed publisher-0.13
C:\Anaconda\Anaconda3\lib\site-packages\IPython\nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead. C:\Anaconda\Anaconda3\lib\site-packages\publisher\publisher.py:53: UserWarning: Did you "Save" this notebook before running this command? Remember to save, always save.