Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:21:06+00:00 2026-06-01T03:21:06+00:00

What i wanna do is adding a single colorbar (at the right side of

  • 0

What i wanna do is adding a single colorbar (at the right side of the figure shown below), that will show the colorbar for both subplots (they are at the same scale).

Another thing doesn’t really make sense for me is why the lines I try to draw on the end of the code are not drawn (they are supposed to be horizontal lines on the center of both plots)

Thanks for the help.

Here are the code:

 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)    

And here the figure it makes:
enter image description here

If any more info are needed, just ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:21:07+00:00Added an answer on June 1, 2026 at 3:21 am

    Basically, figure.colorbar() is good for both images, as long as their are not with too different scales. So you could let matplotlib do it for you… or you manually position your colorbar on axes inside the images. Here is how to control the location of the colorbar:

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

    Note ofcourse you can position ax3 as you wish, on the side, on the top, where ever,
    as long as it is in the boundaries of the figure.

    I don’t know why your line2D is not appearing.

    I added to my code before plt.show() the following and everything is showing:

    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)
    

    Color bar on axes
    WithLine

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna place two child un-ordered lists side by side. They are having Class
i wanna add a service behavior(or anything u'll suggest) that will automatically insert the
I wanna show some text (and images) in browser but this text shouldn't be
I wanna show number of rows of table on my website. query is working
I wanna pass parameter to Custom View at runtime. is that possible? <custom.analog.clock.AnalogClock1 android:id=@+id/clock
Good Day . I just wanna ask about adding days in a given date.
I wanna make a text editor but its different other text editors.It will get
I wanna do a application with C# ; it will count correct words and
I wanna know a single MySQL query for solving this problem: I have a
I wanna show the amplitude of real time sound on a UIView by graph.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.