Skip to content

Instantly share code, notes, and snippets.

@marcossilva
Created August 20, 2018 14:24
Show Gist options
  • Save marcossilva/ec923c7debf205ca850f8fa33a624c0c to your computer and use it in GitHub Desktop.
Save marcossilva/ec923c7debf205ca850f8fa33a624c0c to your computer and use it in GitHub Desktop.
Plotly Boxplot, Violin and Scatter Plot Simple Examples
import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
import numpy as np
pd.set_option('display.max_columns', 200)
pd.set_option('display.max_rows', 500)
py.init_notebook_mode(connected=True)
#Produce two random data to plot
y0 = np.random.randn(50)-1
y1 = np.random.randn(50)+1
#Boxplot
trace0 = go.Box(
y=y0,
name="Data Series 1",
boxpoints='all',
jitter=0.3,
pointpos=-1.8
)
trace1 = go.Box(
y=y1,
name="Data Series 2",
boxpoints='all',
jitter=0.3,
pointpos=-1.8
)
data = [trace0, trace1]
layout = go.Layout(title="Sample Boxplot")
fig = go.Figure(data=data, layout=layout)
# py.iplot(fig) #Plot data on the notebook
print(py.plot(fig, output_type='div', include_plotlyjs=False)) #Print the div on the system output
#Violin
fig = {
"data": [{
"type": 'violin',
"y": y0,
"box": {
"visible": True
},
"line": {
"color": 'black'
},
"meanline": {
"visible": True
},
"fillcolor": '#8dd3c7',
"opacity": 0.6,
"x0": 'Data Series 1'
},
{
"type": 'violin',
"y": y1,
"box": {
"visible": True
},
"line": {
"color": 'black'
},
"meanline": {
"visible": True
},
"fillcolor": '#8dd3c7',
"opacity": 0.6,
"x0": 'Data Series 2'
}],
"layout" : {
"title": "",
"yaxis": {
"zeroline": False,
},
"showlegend": False
}
}
# py.iplot(fig, filename = 'violin/basic', validate = False)
print(py.plot(fig, output_type='div', include_plotlyjs=False))
#Scatter Plot
data =[]
data.append(go.Scatter(
x = np.zeros(len(y0)),
y = y0,
mode = 'markers',
name="Data Series 1"
))
data.append(go.Scatter(
x = np.zeros(len(y1))+0.1,
y = y1,
mode = 'markers',
name="Data Series 2"
))
layout = go.Layout(title="Sample Scatter Plot", xaxis=dict(zeroline=False),
yaxis=dict(zeroline=False))
fig = go.Figure(data=data, layout=layout)
# py.iplot(fig)
print(py.plot(fig, output_type='div', include_plotlyjs=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment