F
FRANKIE LOPEZ
Guest
FRANKIE LOPEZ Asks: My plot is nested in another plot, and negative values are saturated using a diverging colormap
When making a plot using plt.subplot, my plot is nested within another plot, leading to an extra set of axes with ranges 0-1 and cutting off my colorbar.
Additionally, when using a diverging colorbar, all negative values are falsely saturated.
I have used this code format before without having an additional set of axes.
Is there another way to make this kind of plot without using plt.subplot if that is causing the issue?
enter image description here
When making a plot using plt.subplot, my plot is nested within another plot, leading to an extra set of axes with ranges 0-1 and cutting off my colorbar.
Additionally, when using a diverging colorbar, all negative values are falsely saturated.
I have used this code format before without having an additional set of axes.
Is there another way to make this kind of plot without using plt.subplot if that is causing the issue?
Code:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib
import cmocean
import h5py
import pandas as pd
import matplotlib.colors as colors
import cartopy
import matplotlib.colormaps as colormaps
july = h5py.File('data/CCS_Colour_Kahru/chl/1km/daily/2016/Daily_Chlorophyll_July.mat','r')
daily_chl_july = july.get('daily_chl')
lat_july = daily_chl_july.get('Latitude')
lon_july = daily_chl_july.get('Longitude')
chl_july = daily_chl_july.get('chl')
date_july = daily_chl_july.get('date')
data_july = daily_chl_july.get('data')
lat_july = np.array(lat_july)
lat_july = lat_july[0]
lon_july = np.array(lon_july)
lon_july = lon_july[0]
chl_july = np.array(chl_july)
inside_lat_july = np.where(lat_july>=(32.9))
lat_july = lat_july[inside_lat_july]
lon_july = lon_july[inside_lat_july]
chl_july = chl_july[inside_lat_july]
inside_lat_july = np.where(lat_july<=(34.05))
lat_july = lat_july[inside_lat_july]
lon_july = lon_july[inside_lat_july]
chl_july = chl_july[inside_lat_july]
inside_lon_july = np.where(lon_july>=(-118.96))
lon_july = lon_july[inside_lon_july]
lat_july = lat_july[inside_lon_july]
chl_july = chl_july[inside_lon_july]
inside_lon_july = np.where(lon_july<=(-117.54))
lon_july = lon_july[inside_lon_july]
lat_july = lat_july[inside_lon_july]
chl_july = chl_july[inside_lon_july]
mean_chl = np.nanmean(chl_july, axis=1)
remove_nan = ~np.isnan(mean_chl)
mean_chl = mean_chl[remove_nan]
chl_july_mean = chl_july[remove_nan]
lat_july_mean = lat_july[remove_nan]
lon_july_mean = lon_july[remove_nan]
for i in range (0,31,1):
day = i+1
chl_now = chl_july_mean[:,i]
remove_nan = ~np.isnan(chl_now)
chl_now_clean1 = chl_now[remove_nan]
lon_now_clean1 = lon_july_mean[remove_nan]
lat_now_clean1 = lat_july_mean[remove_nan]
mean_chl_clean = mean_chl[remove_nan]
if np.size(chl_now_clean1)==0:
continue
anomaly = chl_now_clean1 - mean_chl_clean
print('min')
print(anomaly.min())
print('max')
print(anomaly.max())
fig, ax = plt.subplots(1,1,figsize=(15,10))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent([-118.96,-117.54,32.9,34.05])
im = ax.scatter(lon_now_clean1, lat_now_clean1, norm=colors.TwoSlopeNorm(vmin=-20, vcenter=0, vmax=20), c=anomaly, cmap='PiYG')
ax.add_feature(cfeature.LAND,zorder=4,color='gray')
ax.set_title("Chlorophyll Anomaly 2016 July "+str(day),fontsize=25)
ax.set_xlabel("Longitude",fontsize=20)
ax.set_ylabel("Latitude",fontsize=20)
xticks = np.linspace(-118.96,-117.54,5)
yticks = np.linspace(32.9,34.05,5)
ax.set_xticks(ticks=xticks)
ax.set_yticks(ticks=yticks)
fig.colorbar(im, ax=ax)
im.set_clim(0.1,1.5)
ax.coastlines()
plt.savefig('frankieleelopez/0124anomaly72016'+str(day))
plt.close()
enter image description here