Search code examples
python-3.xmatplotlibnetworkx

networkx output scale problem with matplotlib (re-post)


I'm re-posting this question since I didn't make a good example code in last question.

I'm trying to make a nodes to set in specific location. But I found out that the output drawing is not... fixed. Let me show you the pic.

enter image description here

So this is the one I make with 10 nodes. worked perfectly as I intended. Also it has plt.text on the bottom left.

And here's the other picture

enter image description here

As you can see, something is wrong. plt.text is gone, and USA's location is weird. Actually that location is where DEU is located in the first pic. Both pics use same code.

Now, let me show you some of my code. for spec_df, please download from my gdrive: https://drive.google.com/drive/folders/11X_i5-pRLGBfQ9vIwQ3hfDU5EWIfR3Uo?usp=sharing

auto_flag = 0
spec_df=pd.read_stata("C:\\"Your_file_loc"\\CombinedHS6_example.dta")
#top_10_list = ["USA","CHN","KOR"] (Try for three nodes)
#or
#auto_flag = 1 (Try for 10 nodes)
df_p = spec_df[['partneriso3','tradevalue']]
df_p = df_p.groupby('partneriso3').sum().reset_index()
df_r = spec_df[['reporteriso3','tradevalue']]
df_r = df_r.groupby('reporteriso3').sum().reset_index()
df_r = df_r.rename(columns={'reporteriso3': 'Nation'})
df_r = df_r.rename(columns={'tradevalue': 'tradevalue_r'})
df_p = df_p.rename(columns={'partneriso3': 'Nation'})

df_s = pd.merge(df_r, df_p, on='Nation', how='outer').fillna(0)
df_s["final"] = df_s['tradevalue'] + df_s['tradevalue_r']

if auto_flag == 1:
    df_s = df_s.sort_values(by=['final'], ascending = False).reset_index()
    cut = df_s[:10]
    
else:
    cut = df_s[(df_s['Nation'].isin(top_10_list))]
    
cut['final'] = cut['final'].apply(lambda x: normalize(x, cut['final'].max()))
cut['font_size'] = cut['final'] * 13
cut['final'] = cut['final'] * 1500
top_10_list = list(cut["Nation"])

top10 = spec_df[(spec_df['reporteriso3'].isin(top_10_list))&(spec_df['partneriso3'].isin(top_10_list))]

top10['tradevalue'] = top10['tradevalue'].apply(lambda x: normalize(x, top10['tradevalue'].max()))
top10['tradevalue'] = top10['tradevalue']*10

plt.figure(figsize=(10,10), dpi = 100)

G = nx.from_pandas_edgelist(top10, 'reporteriso3', 'partneriso3', 'tradevalue', create_using= nx.DiGraph())
widths = nx.get_edge_attributes(G,'tradevalue')
pos = {}
pos_cord = [(-0.30779309, -0.26419882), (0.26767895, 0.19524759), (-0.38479095, 0.88179998), (0.33785317, 0.96090914), (0.94090464, 0.40707934), (0.9270665, -0.38403114), (0.41246223, -0.85684049), (-0.32083322, -1.0), (-0.99724456, -0.34947554), (-0.87530367, 0.40950993)]
for t in range(len(top_10_list)):
    if top_10_list == "":
        continue
    else:
        pos[top_10_list[t]] = pos_cord[t]



pos_nodes = nudge(pos, 0, 0.12)

nx.draw_networkx_edges(G,pos, width=list(widths.values()), edge_color = '#9ECAE4')
nx.draw_networkx_nodes(G, pos=pos, nodelist = cut['Nation'], node_size= cut['final'], node_color ='#AB89EF', edgecolors ='#000000')
nx.draw_networkx_labels(G,pos_nodes, font_size=15)
plt.text(-1.15,-1.15,s='hs : ')
plt.savefig(location,dpi=300)

Sorry for the crude code. But I want to ask that I'm using fixed coordinates. So nodes are not supposed to move there location. So I think the plt's size is kinda interacting with the contents...? But I don't know how it does that.

Could anyone enlighten me please? This drives me crazy...


Solution

  • Thanks to @Paul Brodersen's comment, I found a way to fix the location.

    I just added these codes in my codes.

    fig = plt.figure(figsize=(10,10), dpi = 100)
    axes = fig.add_axes([0,0,1,1])
    axes.set_xlim([-1.3,1.3])
    axes.set_ylim([-1.3,1.3])
    

    Thank you for the help again!