计算化学公社

标题: 使用简单的python代码绘制能量折线图 [打印本页]

作者
Author:
chuan437    时间: 2024-9-26 14:33
标题: 使用简单的python代码绘制能量折线图
本帖最后由 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()  # 显示图形
复制代码




作者
Author:
renzhogn424    时间: 2024-9-26 21:10
我试了下,能画出来,不过会报错,不知道哪里不对。
---> 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
作者
Author:
chuan437    时间: 2024-9-27 07:39
renzhogn424 发表于 2024-9-26 21:10
我试了下,能画出来,不过会报错,不知道哪里不对。
---> 25         plt.hlines(list1, reaction_coordin ...

有可能是python或matplotlib版本的问题,我机器上运行没有这个错误
作者
Author:
乐平    时间: 2024-9-27 09:04
renzhogn424 发表于 2024-9-26 21:10
我试了下,能画出来,不过会报错,不知道哪里不对。
---> 25         plt.hlines(list1, reaction_coordin ...

从报错的信息来看,可能你输入的不是列表,而是单个整数值
作者
Author:
chuan437    时间: 2024-9-28 10:03
本帖最后由 chuan437 于 2024-9-28 10:31 编辑

社长你好:

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




作者
Author:
王二葛    时间: 2024-9-28 10:52

点“<>”图标插入代码

  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()  # 显示图形
复制代码









欢迎光临 计算化学公社 (http://bbs.keinsci.com/) Powered by Discuz! X3.3