首页 > 解决方案 > 标签位置和大小

问题描述

我有一个基于大小的标记节点网络。但是我不能让标签变小并且脱离节点并且不重叠。

import networkx as nx
import matplotlib.pyplot as plt

"Create a Blank Graph Page"
G = nx.Graph()

"Single Node"
#G.add_node(1)
#G.add_node(2)

"Single Edge"
#G.add_edge(1,2)

"Many Nodes"
NxNodeList = ["fish","water","and","in","plant","of","to","aquarium","rock","blue","relationship","tank","are","is","swimming","color","for","yellow","coral","with","good","other","ecosystem","different","environment","fishtank","that","between","green","need","be","filter","goldfish","on","each","orange","living","can","oxygen","school","small","there","it","life","live","red","rule","see","together","dependent","many","species","they","family","gravel","have","pebbles","same","algae","all","around","community","don't","provides","underwater","decorations","eats","feed","food","must","none","some","survive","animals","big","cleans","grass","habitat","hide","home","keeps","know","large","like","mates","reaction","reef","saltwater","sand","so","stone","system","this","types","white","alive","aquatic","filled","from","glass","group","has","humans","nature","or","seaweed","their","them","tree","air","allows","along","beautiful","bottom","breathe","by","cannot","circulating","cooperating","direction","exist","friendly","give","grey","harmony","into","items","light","love","made","makes","mom","ocean","owner","person","place","places","plastic","produce","sea","structure","sure","symbiosis","things","through","up","variety","1","2","3","act","action","applies","as","away","because","broke","bunch","caretakers","clear","coexisting","creatures","dad","decoration","do","either","excellent","flower","form","full","getting","great","greenery","grouper","grow","healthy","helping","homes","how","inside","interact","just","lets","lighting","lights","little","marriage","me","merge","more","multiple","near","not","part","pet","picture","pipe","plankton","plants","pretty","pump","react","rely","schools","sealife","seaweeds","seem","seen","shelter","similar","smaller","space","still","supply","survival","swim","symbiotic","then","therefore","unity","varying","vase","very","waste","well","without","yet","6","able","adapt","adds","aerator","alabaster","alone","also","any","apart","apparently","appear","appears","applicable","archive","arrived","at","attention","awesome","baby","back","based","being","belong","best","biology","bit","blend","boring","bright","bubbles","builded","bushes","called","captivity","carnivorous","cave","chain","change","cleaning","coexistence","cohabiting","collar","comfortable","compatible","complex","conditions","confused","connected","contrast","cool","create","decor","designs","dippy","direct","dishes","distance","distract","docile","dropped","ecological","enjoy","enjoyment","entertain","essential","etc","evenly","everything","exhale","fake","falls","farmer","fauna","feel","female","few","find","fishbowl","fishing","floor","flowing","flows","freely","fresh","freshwater","functioning","gang","gather","get","gills","go","golden","grew","groovy","groups","growing","hand","happy","he","help","helps","here","hides","hiding","hippy","hold","holding","holds","holes","homeowners","household","idea","imprison","improve","including","incredibly","inductive","interested","interesting","job","join","jug","kinds","larger","leaves","lit","lonely","lots","loving","lucky","maintain","male","marine","matter","may","maybe","medium","most","motion","multicolored","needed","no","nome","nothing","office","offspring","ok","one","ones","organisms","ornamental","ornaments","others","outside","parent","partners","parts","peacefully","peer","perspective","pink","pitcher","plantlife","play","pot","pots","predators","principle","prisoners","pvc","real","really","reed","relies","rest","right","salinity","salt","science","screensaver","seabed","seagrass","seeing","settled","several","share","shark","shine","sibling","side","single","size","smooth","someone","spaced","stay","stripe","substrate","supplies","supplying","supports","sweet","take","tandem","temperature","than","thermometer","these","think","three","thrive","tolerate","too","top","toward","trees","tubes","under","undersea","understand","unsure","upon","used","uses","usually","various","vegetation","viewed","wallpaper","walls","want","what","which","work","world","you","your"]
G.add_nodes_from(NxNodeList)

node_sizes = [404,154,79,67,62,61,61,56,49,41,41,40,32,30,28,26,24,23,21,20,19,19,17,16,16,16,16,15,15,15,14,14,14,14,13,13,12,11,11,11,11,11,10,10,10,10,10,10,10,9,9,9,9,8,8,8,8,8,7,7,7,7,7,7,7,6,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,4,4,4,4,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]



"Draw and Show Graph"
nx.draw(G,with_labels=True,node_size=node_sizes)
plt.savefig("NetworkGraph_part1.png")

这造成了这种混乱: 在此处输入图像描述

标签: pythonlabelnetworkx

解决方案


您必须figsize同时指定 以dpi使绘图足够大并具有适当的分辨率。对于文本的大小,指定更小font_size。例如,相关代码如下:-

fig, ax = plt.subplots(figsize=(12,9), dpi=360)
nx.draw(G, with_labels=True, node_size=node_sizes, font_size=4)

推荐阅读