Plotly: How to display charts in Spyder? - python

Since November 2015, plotly is Open-Source and available for python. https://plot.ly/javascript/open-source-announcement/
When trying to do some plots offline, these work in iPython Notebook (version 4.0.4)
But if I try to run them in Spyder (version 2.3.8), i just get the following output:
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>
There's something wrong in my code or the iPython Terminal of Spyder still doesn't support this?
Here goes the example code (taken from https://www.reddit.com/r/IPython/comments/3tibc8/tip_on_how_to_run_plotly_examples_in_offline_mode/)
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *
init_notebook_mode()
trace0 = Scatter(
x=[1, 2, 3, 4],
y=[10, 11, 12, 13],
mode='markers',
marker=dict(
size=[40, 60, 80, 100],
)
)
data = [trace0]
layout = Layout(
showlegend=False,
height=600,
width=600,
)
fig = dict( data=data, layout=layout )
iplot(fig)

If you'd like to develop your plotly figures in Spyder, perhaps because of Spyders superb variable explorer, you can easily display a non-interactive image by just running fig.show(). Note that this is for newer versions of plotly where you don't have to worry about iplot and plotly.offline.
And if you'd like display your figure in the browser as a fully interactive version, just run:
import plotly.io as pio
pio.renderers.default='browser'
Now your figure will be displayed in your default browser.
To switch back to producing your figure in Spyder, just run:
import plotly.io as pio
pio.renderers.default='svg'
You can check other options too using pio.renderers?:
Renderers configuration
-----------------------
Default renderer: 'svg'
Available rendere <...> wser', 'firefox', 'chrome', 'chromium', 'iframe',
'iframe_connected', 'sphinx_gallery']
You'll find even more details here under Setting the default renderer
Here's a detailed example
Code:
import plotly.graph_objects as go
import plotly.io as pio
#pio.renderers.default = 'svg'
pio.renderers.default = 'browser'
x = ['Product A', 'Product B', 'Product C']
y = [20, 14, 23]
fig = go.Figure(data=[go.Bar(
x=x, y=y,
text=y,
textposition='auto',
)])
fig.show()
Plot:
System info:
Python 3.7.6
Spyder 3.3.1
Plotly 3.2.0

importing plot instead iplot (and changing the last line from iplot(fig) to plot(fig) resolved the problem, at least in python 3:
from plotly.offline import download_plotlyjs, init_notebook_mode, plot
from plotly.graph_objs import *
init_notebook_mode()
trace0 = Scatter(
x=[1, 2, 3, 4],
y=[10, 11, 12, 13],
mode='markers',
marker=dict(
size=[40, 60, 80, 100],
)
)
data = [trace0]
layout = Layout(
showlegend=False,
height=600,
width=600,
)
fig = dict( data=data, layout=layout )
plot(fig)
But instead you could do the following, which is slightly easier:
import plotly
import plotly.graph_objs
plotly.offline.plot({
"data": [
plotly.graph_objs.Scatter( x=[1, 2, 3, 4],
y=[10, 11, 12, 13], mode='markers',
marker=dict(
size=[40, 60, 80, 100]))],
"layout": plotly.graph_objs.Layout(showlegend=False,
height=600,
width=600,
)
})

Related

I have been trying to display histogram on google colab but it is not showing

I have been trying to display histogram on google colab but it is not showing. Below is the code that is not displaying:
//imports
from chart_studio import plotly
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go
// code starts from here
trace = go.Histogram(
x = data.defects,
opacity = 0.75,
name = "Defects",
marker = dict(color = 'green'))
hist_data = [trace]
hist_layout = go.Layout(barmode='overlay',
title = 'Defects',
xaxis = dict(title = 'True - False'),
yaxis = dict(title = 'Frequency'),
)
fig = go.Figure(data = hist_data, layout = hist_layout)
iplot(fig) //This is not displaying anything
Try adding this before your code:
%matplotlib inline
import matplotlib.pylab as plt

Placing plotly graphics into a leaflet

Im using the Plotly library for an interactive map in python. However, when you use this library it opens it up in chrome when I want to place the figure onto a leaflet to be used in TKinter
from tkinter import *
import csv
import sys
import plotly.graph_objects as go
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
mapbox_access_token = "pk.eyJ1IjoiY21jY3Vza2VyMTMyMiIsImEiOiJja254YjA5emEwb21rMnB0Z3Nib285OGNkIn0.PL7Nx-Q1XgSO5PBSdI2w2Q"
fig = go.Figure(go.Scattermapbox(
lat=['54.1533'],
lon=['-6.0663'],
mode='markers',
marker=go.scattermapbox.Marker(
size=14
),
text=['Mournes'],
))
fig.update_layout(
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=go.layout.mapbox.Center(
lat=54,
lon=-6
),
pitch=0,
zoom=9
)
)
fig.show()
f = fig

Share all x-axes of a subplot (rows and columns)

I try to share all x-axes of a subplot structure with several columns, but I can't get the solution. With 'share_xaxes=True' only the x-axes of the same row are linked, and I am not able to change the 'xaxis' paramater from the figures in the subplot. Any idea?
In the Plotly documentation you can see that the axes have an attribute called scaleanchor (see https://plot.ly/python/reference/#layout-xaxis-scaleanchor). You can use it to connect as many axes as you like. I tested it out on a simple subplot with 2 rows and 2 columns where all x-axes are connected:
import plotly.plotly as py
import plotly.graph_objs as go
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
def create_figure():
trace1 = go.Scatter(
x=[1, 2, 3],
y=[2, 3, 4]
)
trace2 = go.Scatter(
x=[1, 2, 3],
y=[5, 5, 5],
xaxis='x2',
yaxis='y2'
)
trace3 = go.Scatter(
x=[1, 2, 3],
y=[600, 700, 800],
xaxis='x3',
yaxis='y3'
)
trace4 = go.Scatter(
x=[1, 2, 3],
y=[7000, 8000, 9000],
xaxis='x4',
yaxis='y4'
)
data = [trace1, trace2, trace3, trace4]
layout = go.Layout(
xaxis=dict(
domain=[0, 0.45],
anchor='y'
),
xaxis2=dict(
domain=[0.55, 1],
anchor='y2',
scaleanchor='x'
),
xaxis3=dict(
domain=[0, 0.45],
anchor='y3',
scaleanchor='x'
),
xaxis4=dict(
domain=[0.55, 1],
anchor='y4',
scaleanchor='x'
),
yaxis=dict(
domain=[0, 0.45],
anchor='x'
),
yaxis2=dict(
domain=[0, 0.45],
anchor='x2'
),
yaxis3=dict(
domain=[0.55, 1],
anchor='x3'
),
yaxis4=dict(
domain=[0.55, 1],
anchor='x4'
)
)
fig = go.Figure(data=data, layout=layout)
return fig
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
html.Div(children='''
Dash: A web application framework for Python.
'''),
dcc.Graph(
id='example-graph',
figure=create_figure()
)
])
if __name__ == '__main__':
app.run_server(debug=True)
I know this post is old, but maybe this can help someone else:
Just use the option shared_xaxes = 'all' when you create subplots with make_subplots() and all x-axes will be shared.

How to remove plot margin in plotly tools.make_subplots?

I want to remove the margin of the subplot: tools.make_subplots ( see code plot g01). horizontal_spacing or vertical_spacing does not help.
For a normal plot I can do this with go.Margin(l=0, r=0 ..). (see plot g11 in code).
I just want the same behavior for subplots. Anyone, any Idea?
import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
import base64
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go
from collections import deque
import numpy as np
import random
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Graph(id='g01',),
dcc.Interval(id='graph-update', interval= 1000 * 1),
dcc.Graph(id='g11',),
dcc.Interval(id='graph-update', interval= 1000 * 1),
], style={
'margin-left' : '0',
'margin-right' : '0',
'padding-left' : '0',
'padding-right' : '0',
})
#app.callback(Output('g01', 'figure'),
events=[Event('graph-update', 'interval')])
def update_graph_bar():
trace1 = go.Bar( x=[1, 2, 3], y=[4, 5, 6], showlegend=False)
trace2 = go.Scatter( x=[1, 2, 3], y=[4, 5, 6], showlegend=False)
fig = tools.make_subplots(rows = 1, cols = 2, specs = [[{}, {}]],
horizontal_spacing = 0.00,
vertical_spacing = 0.00,
)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig['layout'].update(height=250, width=660,)
return fig
#app.callback(Output('g11', 'figure'),
events=[Event('graph-update', 'interval')])
def update_graph_bar():
data = [
go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
)
]
layout = go.Layout(
autosize=False,
width=500,
height=500,
margin=go.Margin(
l=0,
r=0,
b=0,
t=0,
pad=0
),
paper_bgcolor= '#7f7f7f',
plot_bgcolor= '#c7c7c7',
)
fig = go.Figure(data=data, layout=layout)
return fig
IP = '192.168.1.1'
if __name__ == '__main__':
app.run_server(debug=True, host=IP,port= 1111)
Ok found it indirectly in cufflinks wrapper :):
tools_by_cufflink
fig['layout'].update(margin=dict(l=0,r=0,b=0,t=0))
Use this one,I guess this'll fulfill your requirement
fig.update_layout(font_size=16, margin=dict(l=0,r=0,b=0,t=0), mapbox_style =
"open-street-map")

Plotly legend next to each subplot, Python

After noticing that there was no answer to this question at the moment, I would like to know if anyone has an idea how to:
Have a legends for each subplot.
Group legends by name. (Ex: for different subplots, all have the same two curves but with different values).
Here's my Plotly script:
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go
import plotly
nom_plot=[]
trace1 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6],name='1',showlegend=True)
nom_plot.append('GRAPH 1')
trace2 = go.Scatter(x=[20, 30, 40], y=[50, 60, 70],name='2',yaxis='y2')
nom_plot.append('GRAPH 2')
trace3 = go.Scatter(x=[300, 400, 500], y=[600, 700, 800],showlegend=False)
nom_plot.append('GRAPH 3')
trace4 = go.Scatter(x=[4000, 5000, 6000], y=[7000, 8000, 9000])
nom_plot.append('GRAPH 4')
trace5 = go.Scatter(x=[20, 30, 40], y=[50, 60, 70])
nom_plot.append('GRAPH 5')
print(trace1)
fig = tools.make_subplots(rows=4, cols=2, subplot_titles=(nom_plot))
fig.append_trace(trace1, 1, 1)
fig['layout']['xaxis1'].update(title='xaxis 1 title')
fig.append_trace(trace2, 1, 1)
fig.append_trace(trace3, 2, 1)
fig.append_trace(trace4, 2, 2)
fig['layout']['yaxis3'].update(title='yaxis 3 title')
fig.append_trace(trace5, 3, 1)
fig['layout']['yaxis2'].update(
overlaying='y1',
side='right',
anchor='x1',
# domain=[0.15, 1],
range=[2, 6],
# zeroline=False,
showline=True,
showgrid=False,
title='yaxis 3 title'
)
fig['layout'].update(height=1000, width=1000, title='Multiple Subplots' +' with Titles')
plotly.offline.plot(fig, filename='multiple-y-subplots6.html')
This what I obtain (Using Plotly Script above):
And this is what I want (Made by Pygal):
The solution is to create an HTML file that merge sevral charts offline rendered as html files:
import plotly
import plotly.offline as py
import plotly.graph_objs as go
fichier_html_graphs=open("DASHBOARD.html",'w')
fichier_html_graphs.write("<html><head></head><body>"+"\n")
i=0
while 1:
if i<=40:
i=i+1
#______________________________--Plotly--______________________________________
color1 = '#00bfff'
color2 = '#ff4000'
trace1 = go.Bar(
x = ['2017-09-25','2017-09-26','2017-09-27','2017-09-28','2017-09-29','2017-09-30','2017-10-01'],
y = [25,100,20,7,38,170,200],
name='Debit',
marker=dict(
color=color1
)
)
trace2 = go.Scatter(
x=['2017-09-25','2017-09-26','2017-09-27','2017-09-28','2017-09-29','2017-09-30','2017-10-01'],
y = [3,50,20,7,38,60,100],
name='Taux',
yaxis='y2'
)
data = [trace1, trace2]
layout = go.Layout(
title= ('Chart Number: '+str(i)),
titlefont=dict(
family='Courier New, monospace',
size=15,
color='#7f7f7f'
),
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
yaxis=dict(
title='Bandwidth Mbit/s',
titlefont=dict(
color=color1
),
tickfont=dict(
color=color1
)
),
yaxis2=dict(
title='Ratio %',
overlaying='y',
side='right',
titlefont=dict(
color=color2
),
tickfont=dict(
color=color2
)
)
)
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='Chart_'+str(i)+'.html',auto_open=False)
fichier_html_graphs.write(" <object data=\""+'Chart_'+str(i)+'.html'+"\" width=\"650\" height=\"500\"></object>"+"\n")
else:
break
fichier_html_graphs.write("</body></html>")
print("CHECK YOUR DASHBOARD.html In the current directory")
Result:
I used two side by side Div elements to emulate Plotly subplot. Doing this way, we have independent legends. However, if we want to share an axis, we should do it manually:
app.layout = html.Div(children=[
html.Div(['YOUR FIRST GRAPH OBJECT'],
style = {'float':'left', 'width':'49%'}) ,
html.Div(['YOUR SECOND GRAPH OBJECT'],
style = {'float':'right', 'width':'49%'})
])

Categories