Welcome to sphinx-plotly-directive’s documentation!

Examples

basic

Source:

.. plotly:: 

   import plotly.express as px

   px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

Output:

import plotly.express as px

px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

(Source code, html)

doctest

Source:

.. plotly:: 

   >>> import plotly.express as px
   >>> px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

Output:

>>> import plotly.express as px
>>> px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

(Source code, html)

function

test_func.py

import plotly.express as px


def func():
    return px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

Source:

.. plotly:: examples/test_func.py func

Output:

import plotly.express as px


def func():
    return px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

(Source code, html)

fig-vars

Single

With fig-vars option, you can render a plotly figure assigned in a variable.

Source:

.. plotly:: 
   :fig-vars: fig1

   import plotly.express as px

   fig1 = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

Output:

import plotly.express as px

fig1 = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

(Source code, html)

Multiple

You can specify multiple variables.

Source:

.. plotly:: 
   :fig-vars: fig1, fig2

   import plotly.express as px

   fig1 = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
   fig2 = px.scatter(x=[4, 3, 2, 1, 0], y=[0, 1, 4, 9, 16])

Output:

import plotly.express as px

fig1 = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig2 = px.scatter(x=[4, 3, 2, 1, 0], y=[0, 1, 4, 9, 16])

(Source code, html, html)

(html)

(html)

precode

By default, the following code will be executed before running each code block. This allows to use np, plotly, go, and px without importing them.

import numpy as np
import plotly
import plotly.graph_objects as go
import plotly.express as px

Source:

.. plotly:: 
   :fig-vars: fig1, fig2

   x = np.arange(5)
   y = x ** 2

   title = "plotly version: {}".format(plotly.__version__)
   fig1 = go.Figure(go.Scatter(x=x, y=y), layout=dict(title=title))
   fig2 = px.scatter(x=x, y=y, title=title)

Output:

x = np.arange(5)
y = x ** 2

title = "plotly version: {}".format(plotly.__version__)
fig1 = go.Figure(go.Scatter(x=x, y=y), layout=dict(title=title))
fig2 = px.scatter(x=x, y=y, title=title)

(Source code, html, html)

(html)

(html)

iframe-size

Source:

.. plotly:: 
   :iframe-width: 500px
   :iframe-height: 300px

   import plotly.express as px

   px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

Output:

import plotly.express as px

px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

(Source code, html)

You can set the default iframe-width and iframe-height by specifying plotly_iframe_width (default: "100%") and plotly_iframe_height (default: "500px") in conf.py.

# conf.py

plotly_iframe_width = "500px"
plotly_iframe_height = "300px"

show

Source:

.. plotly:: 

   import plotly.express as px

   fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
   fig.show()

Output:

import plotly.express as px

fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()

(Source code, html)

Source:

.. plotly:: 
   :fig-vars: figure

   import plotly.express as px

   figure = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
   figure.show()

Output:

import plotly.express as px

figure = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
figure.show()

(Source code, html)

Indices and tables