Can you please help with the following code to generate a energy level diagram?

Illustration
Emanuel - 2020-10-01T11:12:57+00:00
Question: Can you please help with the following code to generate a energy level diagram?

Hello everyone, I run the following code and generated the attached image. But unfortunately the horizontal bar (horizontal allignment) is missing for most of the cases. How to fix the code? clc clear all close all fig = figure('color','white','position',[40 300 700 475]); hold on; xlim([0 9]); ylim([-8.5 -2.5]); set(gca,'ydir','normal'); set(gca,'ytick',-8.5:0.5:-2.5); ylabelString = get(gca,'yticklabel'); ylabelString{1} = 'HOMO'; ylabelString{end} = 'LUMO'; set(gca,'linewidth',3); set(gca,'yticklabel',ylabelString); ylabel('Energy (eV)'); set(gca,'xtick',[]); set(gca,'xcolor',[1 1 1]); set(gca,'fontweight','bold'); set(gca,'fontsize',12); % each energy transition itransition = 0; energy1 = -8.00; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'I', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -4.43; plot([itransition+0.5 itransition+1],[energy2 energy2],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.57', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90); itransition = 1; energy1 = -7.60; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'II', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -3.96; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.64', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90); itransition = 2; energy1 = -7.35; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'III', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -3.78; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.57', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90); itransition = 3; energy1 = -7.30; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'IV', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -3.74; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.56', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90); itransition = 4; energy1 = -7.28; plot([itransition+0.5 itransition+1],[energy1 energy1],'r','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'V', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -3.72; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.56', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90); itransition = 5; energy1 = -7.28; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'VI', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -3.72; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.56', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90); itransition = 6; energy1 = -7.27; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'VII', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -3.71; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.56', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90); itransition = 7; energy1 = -7.27; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'VIII', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -3.70; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.57', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90); itransition = 8; energy1 = -7.43; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy1-0.25,num2str(energy1,'%.2f'), ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); text(itransition+0.75,energy1-0.5,'IX', ... 'horizontalalignment','center','fontsize',12,'fontweight','bold'); energy2 = -3.87; plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5); text(itransition+0.75,energy2+0.25,num2str(energy2,'%.2f'), ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); h = text(itransition+0.75,(energy1+energy2)/2,'E_g=3.56', ... 'horizontalalignment','center', ... 'fontsize',12,'fontweight','bold'); set(h,'Rotation',90);

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

The horizontal bar is missing for few cases because you are ploting the energy1 values instead of energy2 values for few cases. For itransition 1 to 8, you can replace

plot([itransition+0.5 itransition+1],[energy1 energy1],'k','Linewidth',5);

with

plot([itransition+0.5 itransition+1],[energy2 energy2],'k','Linewidth',5); 

while plotting energy2 values. And also for better readability of values, you can reduce the Linewidth.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!