Reputation: 1346
I'm making some plots of ocean temperature and salinity from data I have pulled from a NetCDF file. They are being used as frames in animations I'm making using the moviepy
library. Packages I'm using:
from netCDF4 import Dataset # reads netCDF file
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap # basemap tools
from datetime import datetime, timedelta #for working with datetimes
import moviepy.editor as mpy # creates animation
from moviepy.video.io.bindings import mplfig_to_npimage # converts map to numpy array
from matplotlib.backends.backend_agg import FigureCanvasAgg # draws canvas so that map can be converted
When I draw the plots by themselves they look fine. Here's an exaple of the salinity map.
# map setup
fig = plt.figure()
fig.subplots_adjust(left=0., right=1., bottom=0., top=0.9)
# Setup the map
m = Basemap(projection='merc', llcrnrlat=-38.050653, urcrnrlat=-34.453367,\
llcrnrlon=147.996456, urcrnrlon=152.457344, lat_ts=20, resolution='h')
# draw stuff
m.drawcoastlines()
m.fillcontinents(color='black')
# plot salt
cs = m.pcolor(lons,lats,np.squeeze(salt), latlon = True ,vmin=salt_min, vmax=salt_max, cmap='viridis')
# plot colourbar
plt.colorbar()
# datetime title
plt.title('Regional - Salinity (PSU)\n' + frame_time.strftime("%Y-%m-%d %H:%M:%S") + ' | ' + str(fname) + '_idx: ' + str(frame_idx))
# stop axis from being cropped
plt.tight_layout()
However I want to plot temperature and salinity side by side but when I do the colour bars and figures don't draw as I would expect.
# set up figure
fig = plt.figure()
fig.subplots_adjust(left=0., right=1., bottom=0., top=0.9)
# Temperature figure
plt.subplot(1, 2, 1)
# Setup the map
m = Basemap(projection='merc', llcrnrlat=-38.050653, urcrnrlat=-34.453367,\
llcrnrlon=147.996456, urcrnrlon=152.457344, lat_ts=20, resolution='h')
# draw stuff
m.drawcoastlines()
m.fillcontinents(color='black')
# plot salt
cs = m.pcolor(lons,lats,np.squeeze(temp), latlon = True ,vmin=temp_min, vmax=temp_max, cmap='plasma')
# plot colourbar
plt.colorbar()
# datetime title
plt.title('Regional - Temperature (Celcius)\n' + frame_time.strftime("%Y-%m-%d %H:%M:%S") + ' | ' + str(fname) + '_idx: ' + str(frame_idx))
# Salinity figure
plt.subplot(1, 2, 2)
# Setup the map
m = Basemap(projection='merc', llcrnrlat=-38.050653, urcrnrlat=-34.453367,\
llcrnrlon=147.996456, urcrnrlon=152.457344, lat_ts=20, resolution='h')
# draw stuff
m.drawcoastlines()
m.fillcontinents(color='black')
# plot salt
cs = m.pcolor(lons,lats,np.squeeze(salt), latlon = True ,vmin=salt_min, vmax=salt_max, cmap='viridis')
# plot colourbar
plt.colorbar()
# datetime title
plt.title('Regional - Salinity (PSU)\n' + frame_time.strftime("%Y-%m-%d %H:%M:%S") + ' | ' + str(fname) + '_idx: ' + str(frame_idx))
# make layout nice
plt.tight_layout()
I'd like them to resemble the original example but side by side. I imagine I'm using the subplot function in correctly but I just can't work out how to fix it. Any ideas? Thanks.
NOTE: Maybe not essential for the question, but I might mention that each frame is being converted to a numpy array as I later pass the image to a function in moviepy
to make the animation.
# convert to array
canvas = FigureCanvasAgg(fig)
canvas.draw()
frame = np.fromstring(canvas.tostring_rgb(), dtype='uint8')
frame = frame.reshape(fig.canvas.get_width_height()[::-1] + (3,))
Here's an example of the output:
Upvotes: 2
Views: 406
Reputation: 535
I think this answer will help you: matplotlib colorbar in each subplot (possible duplicate)
It says to look at this matplotlib example page: https://matplotlib.org/examples/images_contours_and_fields/pcolormesh_levels.html
You can see that the example is specifying ax when using the colorbar-method:
fig.colorbar(im, ax=ax0)
Where ax-variables were assigned previously like so:
fig, (ax0, ax1) = plt.subplots(nrows=2)
Upvotes: 2