jwass / mplleaflet

Easily convert matplotlib plots from Python into interactive Leaflet web maps.
BSD 3-Clause "New" or "Revised" License
522 stars 76 forks source link

Does not work with DiGraph (networkx) #62

Open anderl80 opened 5 years ago

anderl80 commented 5 years ago

mplleaflet version: 0.0.5

import mplleaflet
import networkx as nx
import matplotlib.pyplot as plt

pos = {u'Afghanistan': [66.00473365578554, 33.83523072784668],
 u'Aland': [19.944009818523348, 60.23133494165451],
 u'Albania': [20.04983396108883, 41.14244989474517],
 u'Algeria': [2.617323009197829, 28.158938494487625]}

G = nx.DiGraph() # does not work with DiGraphs

fig, ax = plt.subplots()
G.add_edge('Afghanistan','Aland')
G.add_edge('Albania','Algeria')

nx.draw_networkx_nodes(G,pos=pos,node_size=10,node_color='red',edge_color='k',alpha=.5, with_labels=True)
nx.draw_networkx_edges(G,pos=pos,edge_color='gray', alpha=.1)
nx.draw_networkx_labels(G,pos, label_pos =10.3)

mplleaflet.display(fig=ax.figure)

The example works with Graphs but not with DiGraphs. I get the error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-9c14f555d863> in <module>()
     18 nx.draw_networkx_labels(G,pos, label_pos =10.3)
     19 
---> 20 mplleaflet.display(fig=ax.figure)

~\Anaconda3\envs\gis\lib\site-packages\mplleaflet\_display.py in display(fig, closefig, **kwargs)
    151         plt.close(fig)
    152 
--> 153     html = fig_to_html(fig, **kwargs)
    154 
    155     # We embed everything in an iframe.

~\Anaconda3\envs\gis\lib\site-packages\mplleaflet\_display.py in fig_to_html(fig, template, tiles, crs, epsg, embed_links)
     82     renderer = LeafletRenderer(crs=crs, epsg=epsg)
     83     exporter = Exporter(renderer)
---> 84     exporter.run(fig)
     85 
     86     attribution = _attribution + ' | ' + tiles[1]

~\Anaconda3\envs\gis\lib\site-packages\mplleaflet\mplexporter\exporter.py in run(self, fig)
     49             import matplotlib.pyplot as plt
     50             plt.close(fig)
---> 51         self.crawl_fig(fig)
     52 
     53     @staticmethod

~\Anaconda3\envs\gis\lib\site-packages\mplleaflet\mplexporter\exporter.py in crawl_fig(self, fig)
    116                                        props=utils.get_figure_properties(fig)):
    117             for ax in fig.axes:
--> 118                 self.crawl_ax(ax)
    119 
    120     def crawl_ax(self, ax):

~\Anaconda3\envs\gis\lib\site-packages\mplleaflet\mplexporter\exporter.py in crawl_ax(self, ax)
    136                     self.draw_text(ax, artist)
    137             for patch in ax.patches:
--> 138                 self.draw_patch(ax, patch)
    139             for collection in ax.collections:
    140                 self.draw_collection(ax, collection)

~\Anaconda3\envs\gis\lib\site-packages\mplleaflet\mplexporter\exporter.py in draw_patch(self, ax, patch, force_trans)
    225                                 pathcodes=pathcodes,
    226                                 style=linestyle,
--> 227                                 mplobj=patch)
    228 
    229     def draw_collection(self, ax, collection,

~\Anaconda3\envs\gis\lib\site-packages\mplleaflet\leaflet_renderer.py in draw_path(self, data, coordinates, pathcodes, style, offset, offset_coordinates, mplobj)
    123             else:
    124                 data = [c.tolist() for c in data]
--> 125             rings = list(iter_rings(data, pathcodes))
    126 
    127             if style['facecolor'] != 'none':

~\Anaconda3\envs\gis\lib\site-packages\mplleaflet\utils.py in iter_rings(data, pathcodes)
     12             ring.append(point)
     13         else:
---> 14             raise ValueError('Unrecognized code: {}'.format(code))
     15 
     16     if len(ring):

ValueError: Unrecognized code: S