如何在matplotlib脚本中增加一个情节

问题描述 投票:-1回答:1

enter image description here我的matplotlib脚本绘制了一个文件 "band.hdf5",它是hdf5格式的,使用了

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

我想在这里多加一个hdf5文件 "band-new.hdf5",这样输出的图形右边就会多一个新文件的图形。"band-new.hdf5 "的Y轴标签应该避免使用,而X轴标签应该是两个文件共同的。

脚本的标题是这样的:"Band-new.hdf5 "的Y轴标签应该避免,X轴标签应该是两个文件共同的。

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

这篇稿子摘自公认的答案

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

matplotlib hdf5
1个回答
1
投票

这是你需要的解决方案吗?

我把你分享的代码,经过改编,从你分享的数据中并排画出两张图。

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')

最后的图是这样的。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.