鱼C论坛

 找回密码
 立即注册
查看: 1735|回复: 3

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

[复制链接]
发表于 2020-6-28 18:24:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import networkx as nx
import matplotlib.pyplot as plt

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

for a in num:
    start=dic[a][0]
    end=dic[a][1]
    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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-28 18:45:29 | 显示全部楼层


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


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-28 22:23:58 | 显示全部楼层
那请问大神,有啥别的模块可以画图加边注释吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-28 22:45:55 | 显示全部楼层
还有个问题求问大神,为什么我外加的循环,画出来的第二个图就会出现 同一个节点,多次出现的问题,以下是我外加循环后的代码
import networkx as nx
import matplotlib.pyplot as plt

for a in [[3,36,45],[3,45,38]]:
    edgelist=[]
    for b in a:
        start=dictionary[b][0]
        end=dictionary[b][1]
        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']]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-20 07:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表