--- title: You can use Plotly too! authors: - resident_plotly_advocate tags: - plotly - example created_at: 2017-04-20 updated_at: 2017-04-20 tldr: Using plotly inside a knowledge post is as simple as can be! ---

In [3]:
import matplotlib.pyplot as plt
import plotly.offline as plotly
import plotly.graph_objs as go
import numpy as np

plotly.init_notebook_mode(connected=True) # Use connected=False to embed javascript in notebook


A simple plot converted from a matplotlib figure


In [5]:
plt.plot([1,2,3], [4,5,6])
plotly.iplot_mpl(plt.gcf())


A simple plotly linechart


In [6]:
import plotly.graph_objs as go

# 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

# Create traces
trace0 = go.Scatter(
    x = random_x,
    y = random_y0,
    mode = 'markers',
    name = 'markers'
)
trace1 = go.Scatter(
    x = random_x,
    y = random_y1,
    mode = 'lines+markers',
    name = 'lines+markers'
)
trace2 = go.Scatter(
    x = random_x,
    y = random_y2,
    mode = 'lines',
    name = 'lines'
)

data = [trace0, trace1, trace2]
plotly.iplot(data, filename='scatter-mode')