13691255250 发表于 2020-6-28 18:24:02

萌新求问各路大神,Python画图如何优化

import networkx as nx
import matplotlib.pyplot as plt

edgelist=[]
num =
dic={3: [['O', 'H2'], ['H', 'OH']],36: [['H', 'O2'], ['HO2']],45: [['H', 'HO2'], ['O2', 'H2']]}

for a in num:
    start=dic
    end=dic
    for b in start:
      for c in end:
            edgelist.append((b,c))
G=nx.DiGraph()
G.add_edges_from(edgelist)
nx.draw_networkx(G, pos=None, arrows=True, with_labels=True)
plt.show()
   

求问,怎么在输出的图中,每条边上加号码,如'O'到'OH'的边上写 3

Twilight6 发表于 2020-6-28 18:45:29



去查了下networkx不支持给边加上注释的,而 matplotlib 虽然可以加注释,但是是通过坐标来添加的


13691255250 发表于 2020-6-28 22:23:58

那请问大神,有啥别的模块可以画图加边注释吗

13691255250 发表于 2020-6-28 22:45:55

还有个问题求问大神,为什么我外加的循环,画出来的第二个图就会出现 同一个节点,多次出现的问题,以下是我外加循环后的代码
import networkx as nx
import matplotlib.pyplot as plt

for a in [,]:
    edgelist=[]
    for b in a:
      start=dictionary
      end=dictionary
      for c in start:
            for d in end:
                edgelist.append((c,d))
               
    G=nx.DiGraph()
    G.add_edges_from(edgelist)
    nx.draw_networkx(G, pos=None, arrows=True, with_labels=True)
    plt.savefig('D:\\%s.png'%a)


其中的那个dictionary=
{1: [['O'], ['O2']], 2: [['O', 'H'], ['OH']], 3: [['O', 'H2'], ['H', 'OH']], 4: [['O', 'HO2'], ['OH', 'O2']], 5: [['O', 'H2O2'], ['OH', 'HO2']], 6: [['O', 'CH'], ['H', 'CO']], 7: [['O', 'CH2'], ['H', 'HCO']], 8: [['O', 'CH2(S)'], ['H2', 'CO']], 9: [['O', 'CH2(S)'], ['H', 'HCO']], 10: [['O', 'CH3'], ['H', 'CH2O']], 11: [['O', 'CH4'], ['OH', 'CH3']], 12: [['O', 'CO'], ['CO2']], 13: [['O', 'HCO'], ['OH', 'CO']], 14: [['O', 'HCO'], ['H', 'CO2']], 15: [['O', 'CH2O'], ['OH', 'HCO']], 16: [['O', 'CH2OH'], ['OH', 'CH2O']], 17: [['O', 'CH3O'], ['OH', 'CH2O']], 18: [['O', 'CH3OH'], ['OH', 'CH2OH']], 19: [['O', 'CH3OH'], ['OH', 'CH3O']], 20: [['O', 'C2H'], ['CH', 'CO']], 21: [['O', 'C2H2'], ['H', 'HCCO']], 22: [['O', 'C2H2'], ['OH', 'C2H']], 23: [['O', 'C2H2'], ['CO', 'CH2']], 24: [['O', 'C2H3'], ['H', 'CH2CO']], 25: [['O', 'C2H4'], ['CH3', 'HCO']], 26: [['O', 'C2H5'], ['CH3', 'CH2O']], 27: [['O', 'C2H6'], ['OH', 'C2H5']], 28: [['O', 'HCCO'], ['H', 'CO']], 29: [['O', 'CH2CO'], ['OH', 'HCCO']], 30: [['O', 'CH2CO'], ['CH2', 'CO2']], 31: [['O2', 'CO'], ['O', 'CO2']], 32: [['O2', 'CH2O'], ['HO2', 'HCO']], 33: [['H', 'O2'], ['HO2']], 34: [['H', 'O2'], ['HO2']], 35: [['H', 'O2'], ['HO2']], 36: [['H', 'O2'], ['HO2']], 37: [['H', 'O2'], ['HO2']], 38: [['H', 'O2'], ['O', 'OH']], 39: [['H'], ['H2']], 40: [['H'], ['H2']], 41: [['H'], ['H2']], 42: [['H'], ['H2']], 43: [['H', 'OH'], ['H2O']], 44: [['H', 'HO2'], ['O', 'H2O']], 45: [['H', 'HO2'], ['O2', 'H2']]
页: [1]
查看完整版本: 萌新求问各路大神,Python画图如何优化