【问题标题】:how to add one more plot in matplotlib script如何在 matplotlib 脚本中再添加一个绘图
【发布时间】:2020-09-19 10:41:22
【问题描述】:

我的 matplotlib 脚本绘制了一个 hdf5 格式的文件“band.hdf5”,其中

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

我想在这里再添加一个 hdf5 文件“band-new.hdf5”,这样输出图的右侧就会有一个新文件的图。 “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

【问题讨论】:

  • 请不要发布大量代码,其中只有一小部分与问题相关。如果您不了解此脚本的作用,那么这不是该问题的正确论坛。
  • 好的,我已经更新了帖子。

标签: matplotlib hdf5


【解决方案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')

最后的图是这样的。

【讨论】:

  • 原始脚本给出的情节如左侧所示,使用新的 band-new.hdf 文件,我需要帖子中更新的情节。文件的格式是 hdf5,这就是我被困在这里的原因。
  • 好的,但 hdf5 只是格式。你可以read it with python 然后你就可以操作数据了。然后您可以绘制数据并将新带向右移动,您将拥有一个包含所有数据的大图,不是吗?否则你已经有了情节和答案,那么问题出在哪里?
  • 我正在尝试制作一个脚本,使其始终读取两个文件(均为 hdf5 格式)并给出此处发布的情节。我通常使用 GNUplot 进行绘图,但我的文件很难做到这一点,所以我正在寻找 matplotlib。
  • 啊好的,那我可以编辑我的答案了。您基本上需要使用子图并在右侧添加第二个图。这个问题很混乱,抱歉
  • 这对我没有帮助。我得到了两个图,但我无法在左图上拟合数据。这是我的数据 (XXXXwe.tl/t-8y4qu5KLgW)。你能访问看看吗?将链接中的 XXXX 替换为 https://。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 2017-01-05
相关资源
最近更新 更多