Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以对Matplotlib Imshow图进行颜色编码?_Python_Matplotlib_Plot - Fatal编程技术网

Python 是否可以对Matplotlib Imshow图进行颜色编码?

Python 是否可以对Matplotlib Imshow图进行颜色编码?,python,matplotlib,plot,Python,Matplotlib,Plot,我正在处理一些2D数据,在本例中,是一个语音谱图,如果可能的话,我想在图中标记语音日记。我已经很快模拟了一种很好的方式,通过在顶部每列使用一个颜色编码模式来直观地指示日记。可以想象,这样的颜色编码也会导致matplotlib.legend()对象出现在旁边 为了生成标签,我想对所有列使用类标签的一维向量(例如[0、0、0、1、1、0、0、0、2、2、0等]) 如果传说中也有这样的故事,那就太酷了 简言之,在Matplotlib中有没有办法以本机方式实现这一点?为了充实作者的评论,下面是我将如何

我正在处理一些2D数据,在本例中,是一个语音谱图,如果可能的话,我想在图中标记语音日记。我已经很快模拟了一种很好的方式,通过在顶部每列使用一个颜色编码模式来直观地指示日记。可以想象,这样的颜色编码也会导致matplotlib.legend()对象出现在旁边

为了生成标签,我想对所有列使用类标签的一维向量(例如[0、0、0、1、1、0、0、0、2、2、0等])

如果传说中也有这样的故事,那就太酷了

简言之,在Matplotlib中有没有办法以本机方式实现这一点?

为了充实作者的评论,下面是我将如何着手实现您所期望的:

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

是的,只需将另一个imshow放在子地块或顶部的轴中。。。。或者只需使用混合变换在y轴坐标中添加一条直线或矩形,在x轴坐标中添加数据坐标:感谢您将其编码出来!我根本不知道
make\u axes\u locatable
实用程序功能。很高兴知道!