python - 将颜色条和线条添加到多个 imshow() 图中

标签 python numpy matplotlib scipy

我有这个源代码:

idx=0
b=plt.psd(dOD[:,idx],Fs=self.fs,NFFT=512)
B=np.zeros((2*len(self.Chan),len(b[0])))
B[idx,:]=20*log10(b[0])

c=plt.psd(dOD_filt[:,idx],Fs=self.fs,NFFT=512)
C=np.zeros((2*len(self.Chan),len(b[0])))
C[idx,:]=20*log10(c[0])

for idx in range(2*len(self.Chan)):
    b=plt.psd(dOD[:,idx],Fs=self.fs,NFFT=512)
    B[idx,:]=20*log10(b[0])

    c=plt.psd(dOD_filt[:,idx],Fs=self.fs,NFFT=512)
    C[idx,:]=20*log10(c[0])

## Calculate the color scaling for the imshow()    
aux1 = max(max(B[i,:]) for i in range(size(B,0)))
aux2 = min(min(B[i,:]) for i in range(size(B,0)))
bux1 = max(max(C[i,:]) for i in range(size(C,0)))
bux2 = min(min(C[i,:]) for i in range(size(C,0)))
scale1 = 0.75*max(aux1,bux1)
scale2 = 0.75*min(aux2,bux2)


fig, axes = plt.subplots(nrows=2, ncols=1,figsize=(7,7))#,sharey='True')
fig.subplots_adjust(wspace=0.24, hspace=0.35)

ii=find(c[1]>frange)[0]

## Making the plots
cax=axes[0].imshow(B, origin = 'lower',vmin=scale2,vmax=scale1)
axes[0].set_ylim((0,2*len(self.Chan)))
axes[0].set_xlabel(' Frequency (Hz) ')
axes[0].set_ylabel(' Channel Number ') 
axes[0].set_title('Pre-Filtered')
cax2=axes[1].imshow(C, origin = 'lower',vmin=scale2,vmax=scale1)
axes[1].set_ylim(0,2*len(self.Chan))
axes[1].set_xlabel(' Frequency (Hz) ')
axes[1].set_ylabel(' Channel Number ')
axes[1].set_title('Post-Filtered')

axes[0].annotate('690nm', xy=((ii+1)/2, len(self.Chan)/2-1), 
        xycoords='data', va='center', ha='right')
axes[0].annotate('830nm', xy=((ii+1)/2, len(self.Chan)*3/2-1 ), 
        xycoords='data', va='center', ha='right')
axes[1].annotate('690nm', xy=((ii+1)/2, len(self.Chan)/2-1), 
        xycoords='data', va='center', ha='right')
axes[1].annotate('830nm', xy=((ii+1)/2, len(self.Chan)*3/2-1 ), 
        xycoords='data', va='center', ha='right')


axes[0].axis('tight')
axes[1].axis('tight')

## Set up the xlim to aprox frange Hz
axes[0].set_xlim(left=0,right=ii)
axes[1].set_xlim(left=0,right=ii)

## Make the xlabels become the actual frequency number
tickslabel=np.zeros((ii))
ticks = r_[0:ii:5]
tickslabel = linspace(0.,2.,size(ticks))
axes[0].set_xticks(ticks)
axes[0].set_xticklabels(tickslabel)
axes[1].set_xticks(ticks)
axes[1].set_xticklabels(tickslabel)


## Draw a line to separate the two different wave lengths, and name each region
l1 = Line2D([0,ii],[28,10],ls=':',color='black')                                    
axes[0].add_line(l1)
axes[1].add_line(l1)

此代码生成此图: The ploted Figure

使 xticks 看起来正确的固定代码已经在代码内部,并且还显示了新的绘图。

如何向这两个子图添加单个颜色条(并为其指定标题)? (它们的比例相同) 该颜色条应占据图形的整个左侧。

在代码中,有一个地方我尝试在两个图中(在同一位置)画一条线,但这些都没有显示。这是为什么?

如果您需要有关我的代码的更多信息(例如输入数据的大小,请询问)。

最佳答案

您的刻度变量似乎全为零:

ticks=np.zeros((ii))

但它应该枚举您想要刻度线所在的 X 位置(以轴坐标表示)。当您调用 set_xticklabels 时,列表会给出每个刻度显示的文本。

下面是一个简单的示例,展示了 xlim、set_xticks 和 set_xticklabels 如何交互:

from pylab import *
x = arange(128*128).reshape((128,128))
matshow(x)
xlim(right=64)
# xticks: where the xticks should go (indexes into x's columns)
xticks = r_[0:64:25]
gca().set_xticks(xticks)
# labels: text to show for each element of xticks
#    here, we apply a multiplier just to show how the
#    labels can differ from the xticks.
labels = ['%.1f' % (x,) for x in xticks * pi / 2]
gca().set_xticklabels(labels)
show()

关于python - 将颜色条和线条添加到多个 imshow() 图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9857340/

相关文章:

python - 使用 numpy 快速迭代像素

Matplotlib:删除所有艺术家和台词

python - 安装 fastcluster 库时遇到问题,numpy 包含错误

python - Seaborn clustermap 颜色条调整

python - 将 Matplotlib 嵌入到 PyQt 中并进行多重绘图

Python - 时间数据与格式不匹配

python - 为什么 pip 在本地与在 docker 容器中的行为不同?

python - 将声明式 DSL 转换为嵌套函数调用

python - 从文件中读取行时继续使用值,直到列表中的下一个键 - python

python - 在 pandas timeseries dataframe 中查找 max since 条件