简体   繁体   中英

Single plot with three colorbars at the bottom

I am relatively new to Python. I am looking to create a single plot of filled contours from three colorbars, like the following: http://www.cpc.ncep.noaa.gov/products/predictions/30day/off15_temp.gif

I have been able to create the three colorbars that are each of the same length as one side of the plot and place them to the left, bottom and right side respectively. My question is two-fold:

  1. How do you resize the colorbar to be shorter than one side of the plot (I have been able to shrink the width, not the length of the colorbar. In addition, I can only shrink the colorbar to be as long as one side of the plot) ?

  2. How do you position multiple colorbars such that they appear side by side at the bottom of the plot (I have yet to see a single solution with more than one colorbar on a single side of the plot) ?

Below is part of my code:

import matplotlib.pyplot as plt
import numpy as np

#import data
#lon and lat are arrays representing longitude and latitude respectively
#prob_above, prob_normal and prob_below are arrays representing the probability of above average, normal and below average temperature or precipitation occurring

clevs = np.arange(40,110,10) #percent
cs_above = plt.contourf(lon, lat, prob_above, clevs)
cs_normal = plt.contourf(lon, lat, prob_normal, clevs)
cs_below = plt.contourf(lon, lat, prob_below, clevs)

cbar_above = plt.colorbar(cs_above, location = 'left')
cbar_normal = plt.colorbar(cs_normal, location = 'bottom')
cbar_below = plt.colorbar(cs_below, location = 'right')

Colorbars are created within an axes. To have full control over where the colorbar resides you may create an axes at the respective position and use the colorbar's cax argument to specify which axes to use to show the colorbar.
To create useful axes, a GridSpec may be helpful, where the main plot spans multiple gridcells and the ratios of the cell heights is quite asymmetric.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

x,y1,y2,y3 = np.random.rand(4,15)

gs = GridSpec(2,3, height_ratios=[15,1])

fig = plt.figure()
# Axes for plot
ax = fig.add_subplot(gs[0,:])
# three colorbar axes
cax1 = fig.add_subplot(gs[1,0])
cax2 = fig.add_subplot(gs[1,1])
cax3 = fig.add_subplot(gs[1,2])

# plot
sc1 = ax.scatter(x, y1, c=y1, cmap="viridis")
sc2 = ax.scatter(x, y2, c=y2, cmap="RdYlGn")
sc3 = ax.scatter(x, y3, c=y3, cmap="copper")

# colorbars
fig.colorbar(sc1, cax=cax1, orientation="horizontal")
fig.colorbar(sc2, cax=cax2, orientation="horizontal")
fig.colorbar(sc3, cax=cax3, orientation="horizontal")

plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM