Skip to content

Commit e0f8d07

Browse files
committed
resolve import error and make graph edges translucent
1 parent a22b6ac commit e0f8d07

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

content/exploratory_notebooks/world_trade/world_trade_network.md

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ import networkx as nx
2525
import pandas as pd
2626
import matplotlib as mpl
2727
import matplotlib.pyplot as plt
28-
from mpl_toolkits.basemap import Basemap as Basemap
28+
try:
29+
from mpl_toolkits.basemap import Basemap as Basemap
30+
importBasemap = True
31+
except:
32+
print("Basemap cannot be imported, so Geographic visualizations will be plotted without underlying cartograph.")
33+
importBasemap = False
2934
```
3035

3136
For the sake of convenience and scope of this tutorial, the data for trade flows of three product categories - Natural Gas (Hs6: 271111), Coffee (Hs6: 090111) and Diamonds (Hs6: 710210) was extracted to three separate CSV files. These are now imported as pandas dataframe, from where they can be converted to NetworkX Graph objects.
@@ -146,36 +151,22 @@ def draw_pretty(G, geo=False):
146151
)
147152
}
148153
if geo:
149-
postemp = lat_long
150-
m = Basemap(
151-
projection="merc",
152-
llcrnrlon=-180,
153-
llcrnrlat=-80,
154-
urcrnrlon=180,
155-
urcrnrlat=80,
156-
lat_ts=0,
157-
resolution="l",
158-
suppress_ticks=True,
159-
)
160-
m.drawcountries(linewidth=1.5)
161-
m.drawstates(linewidth=0.1)
162-
m.drawcoastlines(linewidth=1.5)
154+
postemp=lat_long
163155
latitudes = [lat_long[country][1] for country in lat_long]
164156
longitudes = [lat_long[country][0] for country in lat_long]
165-
mx, my = m(longitudes, latitudes)
157+
if(importBasemap):
158+
m = Basemap(projection='merc',llcrnrlon=-180,llcrnrlat=-80,urcrnrlon=180, urcrnrlat=80, lat_ts=0, resolution='l',suppress_ticks=True)
159+
m.drawcountries(linewidth = 1.5)
160+
m.drawstates(linewidth = 0.1)
161+
m.drawcoastlines(linewidth=1.5)
162+
longitudes, latitudes = m(longitudes, latitudes)
166163
pos = {}
167164
for count, (key, value) in enumerate(postemp.items()):
168-
if key in G.nodes:
169-
pos[key] = (mx[count], my[count])
165+
if(key in G.nodes):
166+
pos[key] = (longitudes[count], latitudes[count])
170167
else:
171-
pos = nx.spring_layout(G, seed=1231)
172-
nx.draw(
173-
G,
174-
pos,
175-
with_labels=True,
176-
node_size=nsize,
177-
node_color=[mapper.to_rgba(i) for i in indeg_dict.values()],
178-
)
168+
pos=nx.spring_layout(G, seed=1231)
169+
nx.draw(G, pos, with_labels=True, node_size=nsize, node_color=[mapper.to_rgba(i) for i in indeg_dict.values()], alpha = 0.7)
179170
plt.show()
180171
```
181172

@@ -359,6 +350,7 @@ def visualize_communities(graph, communities):
359350
with_labels=True,
360351
font_size=15,
361352
font_color="black",
353+
alpha=0.8
362354
)
363355
364356

0 commit comments

Comments
 (0)