Hi All,
I’m very new to Matplotlib and am having some trouble getting a colorbar to be positioned and sized I want it to. A big part of the problem is that I have adapted several examples from the Cookbook and Gallery, just to see if I could roughly approximate what I want to see, and now am having trouble integrating the different pieces. Specifically, I can’t seem to resolve when to use add_subplot vs. add_axes. Below are 2 examples of code. The first one shows correct layout of a data figure and a separate colorbar below it. The colorbar is the correct size, and is located in the right spot. The second example has the correct data mapped in it using the basemap module, but I cannot get the colorbar to move up closer to the figure, or to shrink it. Could someone advise me on this? I’ve looked at the “Artist tutorial”, and although it is very well written, I’m still not sure how to get this done.
Thanks in advance,
Roger
···
Example 1:
#! /usr/bin/python
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
from matplotlib import mpl
Make plot with horizontal colorbar
fig = plt.figure(figsize=(7,8))
ax = fig.add_subplot(111)
‘add_axes’ for color bar
ax1 = fig.add_axes([0.25, .07, 0.5, 0.03]) # [x_loc, y_loc, x_size, y_size]
data = np.clip(randn(250, 250), -1, 1) # DATA FOR SQUARE FIG
ax.imshow(data, interpolation=‘nearest’) # DRAW DATA IN SQUARE FIG
ax.set_title(‘Monthly PCP percentiles for 9-2008’)
########### Colorbar Settings ########
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=0.0, vmax=1.0)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, norm=norm, orientation=‘horizontal’)
cb1.set_label(‘percentile’)
################################
plt.show()
fig.savefig(‘test.png’)
Example 2:
#! /usr/bin/python
“”“taken from geos_demo_2.py”""
from PIL import Image
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib
from matplotlib import mpl
import matplotlib.pyplot as plt
from matplotlib.image import pil_to_array
plot_name = ‘hydro_demo.png’
overlay_color = ‘black’
read in jpeg image to rgb array
pilImage = Image.open(‘wms_mapser.png’)
#data = asarray(pilImage)
data = pil_to_array(pilImage)
data = data[:, :, :] # get data from RGB channels of image
define data region and projection parameters
ll_lon = -125
ll_lat = 39
ur_lon = -108
ur_lat = 54
lon_0 = 0
fig = plt.figure(figsize=(7,8))
#ax = fig.add_axes((0.1,0.1,0.8,0.8))
ax = fig.add_axes((.1,0.1,0.8,0.8))
create Basemap instance for cylindrical equidistant projection, htdro image domain
m = Basemap(projection=‘cyl’, lon_0=lon_0, llcrnrlon=ll_lon, llcrnrlat=ll_lat, urcrnrlon=ur_lon,
urcrnrlat=ur_lat, suppress_ticks=False)
add data
cmap = mpl.cm.cool
m.imshow(data, cmap, interpolation=None)
plt.clim(0, 1)
add a colobar
plt.colorbar(orientation=‘horizontal’)
add timestamp and save
fig = plt.gcf()
ADD FIGURE TEXT
fig.text(x=0.5, y=0.1,
s=‘percentile’,
fontsize=10,
)
fig.set_size_inches((7,7))
ADD A FIGURE TITLE
plt.title(‘Monthly PCP percentiles for 9-2008’,y=1.05,fontsize=12)
plt.show()
#fig.savefig(plot_name)
#print ‘Plot saved to %s’ % (plot_name)