【问题标题】:How to add a single colobar that will show the data from 2 different subplot如何添加将显示来自 2 个不同子图的数据的单个 colobar
【发布时间】:2012-03-28 16:39:19
【问题描述】:

我想做的是添加一个颜色条(在下图的右侧),它将显示两个子图的颜色条(它们的比例相同)。

另一件事对我来说真的没有意义是为什么我试图在代码末尾绘制的线没有被绘制(它们应该是两个图中心的水平线)

感谢您的帮助。

代码如下:

 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
ticks = linspace(0,ii,10)
tickslabel = linspace(0.,frange,10)
for i in range(10):
    tickslabel[i]="%.1f" % tickslabel[i]
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,frange],[28,28],ls='-',color='black')
axes[0].add_line(l1)
axes[1].add_line(l1)    

这里的数字是:

如果需要更多信息,请询问。

【问题讨论】:

    标签: python numpy matplotlib scipy


    【解决方案1】:

    基本上, figure.colorbar() 对两个图像都有好处,只要它们的比例没有太大差异。所以你可以让 matplotlib 为你做这件事......或者你手动将你的颜色条放置在图像内的轴上。下面是如何控制颜色条的位置:

    import numpy as np
    from matplotlib import pyplot as plt
    
    A = np.random.random_integers(0, 10, 100).reshape(10, 10)
    B = np.random.random_integers(0, 10, 100).reshape(10, 10)
    
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    
    mapable = ax1.imshow(A, interpolation="nearest")
    cax = ax2.imshow(A, interpolation="nearest")
    
    # set the tickmarks *if* you want cutom (ie, arbitrary) tick labels:
    cbar = fig.colorbar(cax, ax=None)
    
    fig = plt.figure(2)
    ax1 = fig.add_subplot(121)
    ax2 = fig.add_subplot(122)
    mapable = ax1.imshow(A, interpolation="nearest")
    cax = ax2.imshow(A, interpolation="nearest")
    # on the figure total in precent l    b      w , height 
    ax3 = fig.add_axes([0.1, 0.1, 0.8, 0.05]) # setup colorbar axes. 
    # put the colorbar on new axes
    cbar = fig.colorbar(mapable,cax=ax3,orientation='horizontal')
    
    plt.show()
    

    请注意,当然,您可以根据需要将 ax3 放置在侧面、顶部、任何位置, 只要它在图形的边界内。

    我不知道为什么您的 line2D 没有出现。

    我在 plt.show() 之前的代码中添加了以下内容,一切都在显示:

    from mpl_toolkits.axes_grid1 import anchored_artists
    from matplotlib.patheffects import withStroke
    txt = anchored_artists.AnchoredText("SC",
                                        loc=2,
                                        frameon=False,
                                        prop=dict(size=12))
    if withStroke:
        txt.txt._text.set_path_effects([withStroke(foreground="w",
                                                   linewidth=3)])
    ax1.add_artist(txt)
    
    
    ## Draw a line to separate the two different wave lengths, and name each region
    l1 = plt.Line2D([-1,10],[5,5],ls='-',color='black',lineswidth=10)
    ax1.add_line(l1)
    

    【讨论】:

    • 颜色条方法 realyl 解决了。我只需要使用 subplot_adjust,因为我使用的是轴('tight')并且我希望颜色条在右侧.....关于Line2D,我基本上什么都试过了,就是不显示;/...还有其他显示线条的方法吗?无论如何,非常感谢..
    • 想出了一种显示线条的方法..它与我如何调用轴有关....我所要做的就是更改我将轴添加到的线条: { for (i), ax in np.ndenumerate(axes): l1 = plt.Line2D([0,ii],[28,28],ls='-',color='black') ax.add_line(l1 )}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    相关资源
    最近更新 更多