-1

enter image description hereMy matplotlib script plots a file "band.hdf5", which is in hdf5 format, with

f = h5py.File('band.hdf5',  'r')

I want to add one more hdf5 file "band-new.hdf5" here in such a way that the output plot will have one more plot on right side for new file. Y-axis label should be avoided for "band-new.hdf5" and X-axis label should be common for both file.

The header of the script is this

import h5py
import matplotlib.pyplot as plt 
import warnings
import matplotlib

This script is taken from the accepted answer

https://stackoverflow.com/questions/62099211/how-to-plot-two-case1-hdf5-and-case2-hdf5-files-in-matplotlib-seeking-help-to-c?rq=1

enter image description here

astha
  • 593
  • 5
  • 15

1 Answers1

1

Is this the solution you needed?

I take the code from and adapted it to draw two plots side-to-side from the data you shared.

import h5py
import matplotlib.pyplot as plt 
import warnings
import matplotlib

warnings.filterwarnings("ignore") # Ignore all warnings

cmap = matplotlib.cm.get_cmap('jet', 4)

ticklabels=['A','B','C','D','E']

params = {
'mathtext.default': 'regular',
'axes.linewidth': 1.2,
'axes.edgecolor': 'Black',
'font.family' : 'serif'
}


#get the viridis cmap with a resolution of 3

#apply a scale to the y axis. I'm just picking an arbritrary number here
scale = 10
offset = 0 #set this to a non-zero value if you want to have your lines offset in a waterfall style effect

f_left = h5py.File('band-222.hdf5',  'r')
f_right = h5py.File('band-332.hdf5',  'r')

print ('datasets from left are:')
print(list(f_left.keys()))

print ('datasets from right are:')
print(list(f_right.keys()))

# PLOTTING 
plt.rcParams.update(params)
fig = plt.figure(figsize=(16,8))
ax1 = fig.add_subplot(121)


# LEFT ONE
dist=f_left[u'distance']
freq=f_left[u'frequency']
kpt=f_left[u'path']

lbl = {0:'AB', 1:'BC', 2:'CD', 3:'fourth'}

for i, section in enumerate(dist):
    for nbnd, _ in enumerate(freq[i][0]):
        x = section # to_list() you may need to convert sample to list.
        y = (freq[i, :, nbnd] + offset*nbnd) * scale

        if (nbnd<3):
            color=f'C{nbnd}'
        else:
            color='black'

        ax1.plot(x, y, c=color, lw=2.0, alpha=0.8, label = lbl[nbnd] if nbnd < 3 and i == 0 else None)

ax1.legend()

# Labels and axis limit and ticks
ax1.set_ylabel(r'Frequency (THz)', fontsize=12)
ax1.set_xlabel(r'Wave Vector (q)', fontsize=12)
ax1.set_xlim([dist[0][0],dist[len(dist)-1][-1]])
xticks=[dist[i][0] for i in range(len(dist))]
xticks.append(dist[len(dist)-1][-1])
ax1.set_xticks(xticks)
ax1.set_xticklabels(ticklabels)
# Plot grid
ax1.grid(which='major', axis='x', c='green', lw=2.5, linestyle='--', alpha=0.8)


# RIGHT ONE
ax2 = fig.add_subplot(122)
dist=f_right[u'distance']
freq=f_right[u'frequency']
kpt=f_right[u'path']

lbl = {0:'AB', 1:'BC', 2:'CD', 3:'fourth'}

for i, section in enumerate(dist):
    for nbnd, _ in enumerate(freq[i][0]):
        x = section # to_list() you may need to convert sample to list.
        y = (freq[i, :, nbnd] + offset*nbnd) * scale

        if (nbnd<3):
            color=f'C{nbnd}'
        else:
            color='black'

        ax2.plot(x, y, c=color, lw=2.0, alpha=0.8, label = lbl[nbnd] if nbnd < 3 and i == 0 else None)

ax2.legend()
# remove y axis
ax2.axes.get_yaxis().set_visible(False)
ax2.set_xlabel(r'Wave Vector (q)', fontsize=12)
ax2.set_xlim([dist[0][0],dist[len(dist)-1][-1]])
xticks=[dist[i][0] for i in range(len(dist))]
xticks.append(dist[len(dist)-1][-1])
ax2.set_xticks(xticks)
ax2.set_xticklabels(ticklabels)
# Plot grid
ax2.grid(which='major', axis='x', c='green', lw=2.5, linestyle='--', alpha=0.8)

fig.tight_layout() # Or equivalently,  "plt.tight_layout()"

# Save to pdf
plt.savefig('plots.pdf', bbox_inches='tight')

The final figure is like this.

enter image description here

freerafiki
  • 539
  • 3
  • 10
  • The original script gives plot as shown in left side and with new band-new.hdf file, I need the plot as updated in the post. The format of the files are hdf5 and that why I am stuck here. – astha Jun 01 '20 at 09:27
  • ok, but hdf5 is just the format. You can [read it with python](https://stackoverflow.com/questions/28170623/how-to-read-hdf5-files-in-python) and then you can manipulate the data. Then you can plot the data and shift the band-new to the right and you will have one large plot with all the data, no? Otherwise you already have the plot, and the answer, so where is the problem? – freerafiki Jun 01 '20 at 10:32
  • I am trying to make a script in such a way that it should always read two files (both are hdf5 format) and give the plot like posted here. I usually do the plotting with GNUplot but that is difficult with my files so I am looking for matplotlib. – astha Jun 01 '20 at 10:54
  • ah ok, then I can edit my answer. You basically need to use subplot and add a second plot on the right. The question was very confusing, sorry – freerafiki Jun 01 '20 at 11:39
  • This did not help me. I am getting two plots but I am not able to fit data on left plot. Here are my data (XXXXwe.tl/t-8y4qu5KLgW) . Could you please access and have a look? replace XXXX with https:// in the link. – astha Jun 01 '20 at 13:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215102/discussion-between-elgordorafiki-and-astha). – freerafiki Jun 01 '20 at 13:34