Ad
Plotting Two Figures From Two Xarrays Side By Side
I'm trying to plot two subplots side by side. Each subplot is a variable from xarray, and the plot is a projection.
My plotting function is:
from matplotlib import pyplot as plt
import cartopy.crs as ccrs
import cartopy
def plot_avg(data, **kwargs):
ax = plt.axes(projection=ccrs.Orthographic(central_latitude=-90.0))
ax.coastlines(resolution='10m',zorder=3)
ax.gridlines(color='gray')
ax.add_feature(cartopy.feature.LAND, zorder=1,facecolor=cartopy.feature.COLORS['land_alt1'])
data.plot(ax = ax, transform=ccrs.PlateCarree(),
vmin = 34.4 , vmax=35.81 , levels=17 ,
cmap=Becki, cbar_kwargs={'shrink': 0.3})
And the main code is:
fig = plt.figure()
ax1 = fig.add_subplot(111)
plot_avg(var1)
ax2 = fig.add_subplot(122)
plot_avg(var2)
But for some reason I can't do it, i always get one plot with two colorbars:
What can I do differently? I'm having a hard time with the plot objects from the xarray.
The sample data files for plotting are here: netcdf files
Ad
Answer
You create too many axes here. Make sure to only create the two axes you want to use.
def plot_avg(data, ax, **kwargs):
ax.coastlines(resolution='10m',zorder=3)
ax.gridlines(color='gray')
ax.add_feature(cartopy.feature.LAND, zorder=1,facecolor=cartopy.feature.COLORS['land_alt1'])
data.plot(ax = ax, transform=ccrs.PlateCarree(),
vmin = 34.4 , vmax=35.81 , levels=17 ,
cmap=Becki, cbar_kwargs={'shrink': 0.3})
fig = plt.figure()
ax1 = fig.add_subplot(121, projection=ccrs.Orthographic(central_latitude=-90.0))
plot_avg(var1, ax1)
ax2 = fig.add_subplot(122, projection=ccrs.Orthographic(central_latitude=-90.0))
plot_avg(var2, ax2)
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad