计算化学公社

 找回密码 Forget password
 注册 Register
Views: 769|回复 Reply: 5
打印 Print 上一主题 Last thread 下一主题 Next thread

[辅助/分析程序] 使用简单的python代码绘制能量折线图

[复制链接 Copy URL]

82

帖子

3

威望

1613

eV
积分
1755

Level 5 (御坂)

本帖最后由 chuan437 于 2024-9-28 11:06 编辑

今天这里介绍一段很简单的python代码来绘制能量折线图。

脚本使用说明:
这里最需要注意的是设置list0,其中存储了你的一系列能量。
出图的效果如下图,其中有些设置(例如线型,颜色等)可参照注释自行调整。

代码见附件,绘制能量折线图.py。推荐在jupyter notebook中运行该脚本。
代码如下:


  1. import matplotlib.pyplot as plt

  2. # 假设list0是你的能量列表
  3. list0 = [-685.2534481,-685.3582154,-685.253315]

  4. # 对list0进行处理,得到绘图采用的list1
  5. list1=[]
  6. for i in list0:
  7.     list1.append(i)
  8.     list1.append(i)

  9. # 反应坐标,例如,可以是反应进度或者时间
  10. # 这里我们使用索引作为反应坐标
  11. reaction_coordinates = list(range(len(list1)))

  12. h=(max(list0)-min(list0))*0.05
  13. # 绘制能量折线图
  14. plt.figure(figsize=(10, 5))  # 设置图形的大小
  15. # 绘制连接平台的折线
  16. plt.plot(reaction_coordinates, list1, marker='o', color='red', linestyle='--',label='Energy Change')

  17. # 绘制平台
  18. reaction_coordinates1=[i+1 for i in reaction_coordinates]


  19. for i in range(len(list1)-1):
  20.     if i % 2 == 0:
  21.         plt.hlines(list1[i], reaction_coordinates[i], reaction_coordinates1[i], lw=5, color='black', label='Energy Level' if i==0 else "")
  22.         plt.text(reaction_coordinates[i]+0.5,list1[i]-h, f'{list1[i]:.5f}', ha='center', va='top')

  23. plt.ylim(min(list1) - 5*h, max(list1) + 5*h)
  24. plt.grid(True) #显示网格线
  25. plt.xlabel('Reaction Coordinate')  # 设置x轴标签
  26. plt.xticks([])
  27. plt.ylabel('Energy (Hatree)')  # 设置y轴标签
  28. plt.legend()  # 显示图例
  29. # plt.tight_layout()  # 自动调整子图参数,使之填充整个图像区域
  30. plt.show()  # 显示图形
复制代码



download.png (36.18 KB, 下载次数 Times of downloads: 6)

download.png

绘制能量折线图.py

1.33 KB, 下载次数 Times of downloads: 4

评分 Rate

参与人数
Participants 2
eV +6 收起 理由
Reason
sobereva + 5
lemon_electron + 1 赞!

查看全部评分 View all ratings

140

帖子

0

威望

1633

eV
积分
1773

Level 5 (御坂)

2#
发表于 Post on 2024-9-26 21:10:17 | 只看该作者 Only view this author
我试了下,能画出来,不过会报错,不知道哪里不对。
---> 25         plt.hlines(list1, reaction_coordinates, reaction_coordinates+1, lw=5, color='black', label='Energy Level' if i==0 else "")
     26         plt.text(reaction_coordinates+0.5,list1-h, f'{list1:.5f}', ha='center', va='top')
     28 plt.ylim(min(list1) - 5*h, max(list1) + 5*h)

TypeError: can only concatenate list (not "int") to list

82

帖子

3

威望

1613

eV
积分
1755

Level 5 (御坂)

3#
 楼主 Author| 发表于 Post on 2024-9-27 07:39:57 | 只看该作者 Only view this author
renzhogn424 发表于 2024-9-26 21:10
我试了下,能画出来,不过会报错,不知道哪里不对。
---> 25         plt.hlines(list1, reaction_coordin ...

有可能是python或matplotlib版本的问题,我机器上运行没有这个错误

1155

帖子

0

威望

4429

eV
积分
5584

Level 6 (一方通行)

4#
发表于 Post on 2024-9-27 09:04:06 | 只看该作者 Only view this author
renzhogn424 发表于 2024-9-26 21:10
我试了下,能画出来,不过会报错,不知道哪里不对。
---> 25         plt.hlines(list1, reaction_coordin ...

从报错的信息来看,可能你输入的不是列表,而是单个整数值

82

帖子

3

威望

1613

eV
积分
1755

Level 5 (御坂)

5#
 楼主 Author| 发表于 Post on 2024-9-28 10:03:26 | 只看该作者 Only view this author
本帖最后由 chuan437 于 2024-9-28 10:31 编辑

社长你好:

我仔细检查了一下,我发现把代码复制到帖子里时,中括号和其内部的内容被自动删除了。这是代码出现错误的原因。
请问这是为什么?



135

帖子

1

威望

4134

eV
积分
4289

Level 6 (一方通行)

6#
发表于 Post on 2024-9-28 10:52:13 | 只看该作者 Only view this author

点“<>”图标插入代码

  1. import matplotlib.pyplot as plt

  2. # 假设list0是你的能量列表
  3. list0 = [-685.2534481,-685.3582154,-685.253315]

  4. # 对list0进行处理,得到绘图采用的list1
  5. list1=[]
  6. for i in list0:
  7.     list1.append(i)
  8.     list1.append(i)

  9. # 反应坐标,例如,可以是反应进度或者时间
  10. # 这里我们使用索引作为反应坐标
  11. reaction_coordinates = list(range(len(list1)))

  12. h=(max(list0)-min(list0))*0.05
  13. # 绘制能量折线图
  14. plt.figure(figsize=(10, 5))  # 设置图形的大小
  15. # 绘制连接平台的折线
  16. plt.plot(reaction_coordinates, list1, marker='o', color='red', linestyle='--',label='Energy Change')

  17. # 绘制平台
  18. reaction_coordinates1=[i+1 for i in reaction_coordinates]


  19. for i in range(len(list1)-1):
  20.     if i % 2 == 0:
  21.         plt.hlines(list1[i], reaction_coordinates[i], reaction_coordinates1[i], lw=5, color='black', label='Energy Level' if i==0 else "")
  22.         plt.text(reaction_coordinates[i]+0.5,list1[i]-h, f'{list1[i]:.5f}', ha='center', va='top')

  23. plt.ylim(min(list1) - 5*h, max(list1) + 5*h)
  24. plt.grid(True) #显示网格线
  25. plt.xlabel('Reaction Coordinate')  # 设置x轴标签
  26. plt.xticks([])
  27. plt.ylabel('Energy (Hatree)')  # 设置y轴标签
  28. plt.legend()  # 显示图例
  29. # plt.tight_layout()  # 自动调整子图参数,使之填充整个图像区域
  30. plt.show()  # 显示图形
复制代码




评分 Rate

参与人数
Participants 1
eV +5 收起 理由
Reason
chuan437 + 5 谢谢

查看全部评分 View all ratings

十八介姑娘一蕾花呀,白白介牙齿、红红介嘴唇,得人惜

本版积分规则 Credits rule

手机版 Mobile version|北京科音自然科学研究中心 Beijing Kein Research Center for Natural Sciences|京公网安备 11010502035419号|计算化学公社 — 北京科音旗下高水平计算化学交流论坛 ( 京ICP备14038949号-1 )|网站地图

GMT+8, 2026-2-21 23:55 , Processed in 0.241588 second(s), 24 queries , Gzip On.

快速回复 返回顶部 返回列表 Return to list