Last active
October 12, 2017 16:00
-
-
Save nworbmot/301099c67a87251a4431ff85f4d9c486 to your computer and use it in GitHub Desktop.
Minimal example of plotly working with PyPSA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Based on https://plot.ly/python/network-graphs/ | |
## Copyright plotly team, Tom Brown, Bryn Pickering 2017 | |
## Licensed under the Apache 2.0 Licence. | |
## Working demo: https://pypsa.org/examples/plotly_network.html | |
import plotly.offline as py | |
from plotly.graph_objs import * | |
import pypsa | |
#data available from https://github.com/FRESNA/PyPSA | |
n = pypsa.Network("/home/tom/fias/lib/pypsa/examples/scigrid-de/scigrid-with-load-gen-trafos/") | |
py.init_notebook_mode(connected=True) | |
%matplotlib inline | |
edge_trace = Scatter( | |
x=[], | |
y=[], | |
line=Line(width=0.5,color='#888'), | |
hoverinfo='none', | |
mode='lines') | |
for edge in n.lines.index: | |
x0, y0 = n.buses.at[n.lines.at[edge,"bus0"],"x"],n.buses.at[n.lines.at[edge,"bus0"],"y"] | |
x1, y1 = n.buses.at[n.lines.at[edge,"bus1"],"x"],n.buses.at[n.lines.at[edge,"bus1"],"y"] | |
edge_trace['x'] += [x0, x1, None] | |
edge_trace['y'] += [y0, y1, None] | |
mid_edge_trace = Scatter( | |
x=[0.5*(n.buses.at[n.lines.at[line,"bus0"],"x"]+n.buses.at[n.lines.at[line,"bus1"],"x"]) for line in n.lines.index], | |
y=[0.5*(n.buses.at[n.lines.at[line,"bus0"],"y"]+n.buses.at[n.lines.at[line,"bus1"],"y"]) for line in n.lines.index], | |
text=["line " +line+" has capacity %.1f MW" % n.lines.at[line,"s_nom"] for line in n.lines.index], | |
mode='markers', | |
hoverinfo='text', | |
marker=Marker( | |
opacity=0 | |
)) | |
node_trace = Scatter( | |
x=[], | |
y=[], | |
text=[], | |
mode='markers', | |
hoverinfo='text', | |
marker=Marker( | |
showscale=True, | |
# colorscale options | |
# 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' | | |
# Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu' | |
colorscale='YIGnBu', | |
reversescale=True, | |
color=[], | |
size=[], | |
colorbar=dict( | |
thickness=15, | |
title='Node Connections', | |
xanchor='left', | |
titleside='right' | |
), | |
line=dict(width=2))) | |
load_distribution = n.loads_t.p_set.iloc[0].groupby(n.loads.bus).sum().reindex(n.buses.index,fill_value=0.) | |
for node in n.buses.index: | |
x, y = n.buses.at[node,"x"],n.buses.at[node,"y"] | |
node_trace['x'].append(x) | |
node_trace['y'].append(y) | |
node_trace["text"].append("bus "+ node+" has peak load %.1f MW" % load_distribution.loc[node]) | |
node_trace["marker"]["color"].append("blue") | |
node_trace["marker"]["size"].append(0.1*load_distribution.loc[node]) | |
fig = Figure(data=Data([edge_trace, node_trace, mid_edge_trace]), | |
layout=Layout( | |
title='<br>PyPSA graph (based on SciGRID)', | |
titlefont=dict(size=16), | |
showlegend=False, | |
hovermode='closest', | |
margin=dict(b=20,l=5,r=5,t=40), | |
annotations=[ dict( | |
text="Python code: <a href='https://gist.github.com/nworbmot/301099c67a87251a4431ff85f4d9c486'> https://gist.github.com/nworbmot/301099c67a87251a4431ff85f4d9c486</a>", | |
showarrow=False, | |
xref="paper", yref="paper", | |
x=0.005, y=-0.002 ) ], | |
xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False), | |
yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False))) | |
py.iplot(fig, filename='networkx') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment