我正在处理一些2D数据,在这种情况下,是语音频谱图,如果可能的话,我想在图中标记语音二元化。我很快就模拟出了一种很好的方法,通过在顶部使用每个列的颜色编码模式来直观地指示二分。可以想象,这样的颜色编码也应该导致在侧面有一个matplotlib.legend()对象。
为了生成标签,我想使用类标签的一维向量(例如,对于所有列,0,0,0,1,1,0,0,0,2,2,0等)。
如果图例也使用qualitative colormaps,那就太酷了。
所以简而言之,有没有办法在Matplotlib中实现这一点呢?
发布于 2019-07-16 20:37:56
为了充实@ImportanceOfBeingErnest的评论,下面是我将如何去做你想要的:
from mpl_toolkits.axes_grid1 import make_axes_locatable
# use of `make_axes_locatable` simplifies positioning the
# accessory axes
# Generate data, it would have been nice if you had provided
# these in your question BTW
Ncols, Nlines = 200,50
data = np.random.random(size=(Nlines,Ncols))
class_labels = np.zeros(shape=(Ncols,))
class_labels[50:100] = 1
class_labels[100:150] = 2
class_labels = class_labels.reshape((1,Ncols))
fig, ax = plt.subplots(1,1)
# create new axes on the right and on the top of the current axes.
divider = make_axes_locatable(ax)
class_ax = divider.append_axes("top", size=0.1, pad=0., sharex=ax)
cbar_ax = divider.append_axes("right", size=0.1, pad=0.1)
#plot sonogram
im = ax.imshow(data, cmap='viridis')
fig.colorbar(im, cax=cbar_ax) # sonogram colorbar
# plot diarization classes
class_ax.imshow(class_labels, aspect='auto', cmap='rainbow')
class_ax.set_axis_off()
https://stackoverflow.com/questions/57060849
复制