I am trying to plot separate maps for six different time steps of my data set. My code is shown below.
import matplotlib.pyplot as plt
import time
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from cartopy.util import add_cyclic_point
import cartopy.feature as cfeature
from netCDF4 import Dataset
import numpy as np
#open dataset
myfile = 'gfs20191010.0p25.nc'
fh = Dataset(myfile, mode='r')
#select variables
h5001 = fh.variables['h5'][0:24,:,:]
h5002 = fh.variables['h5'][25:48,:,:]
h5003 = fh.variables['h5'][49:72,:,:]
h5004 = fh.variables['h5'][73:96,:,:]
h5005 = fh.variables['h5'][97:120,:,:]
h5006 = fh.variables['h5'][121:144,:,:]
lats = fh.variables['lat'][:]
lons = fh.variables['lon'][:]
#take mean of 500mb heights for the first 24 hours
day1 = np.mean(h5001,axis=0)
day2 = np.mean(h5002,axis=0)
day3 = np.mean(h5003,axis=0)
day4 = np.mean(h5004,axis=0)
day5 = np.mean(h5005,axis=0)
day6 = np.mean(h5006,axis=0)
fcst_crs = ccrs.PlateCarree()
fig = plt.figure(figsize=(5,5))
ax0 = plt.axes(projection=ccrs.PlateCarree())
ax0.set_extent([230,300,20,90],ccrs.PlateCarree())
ax0.coastlines()
ax0.add_feature(cfeature.BORDERS)
ax0.add_feature(cfeature.STATES)
ax0.set_title('Mean 500mb Heights (dam)', fontsize=12)
fig = plt.subplot(ax0)
mean1 = fig.contour(lons,lats,day1[0,:,:],levels=60,extend='both',transform=fcst_crs)
mean2 = fig.contour(lons,lats,day2[0,:,:],levels=60,extend='both',transform=fcst_crs)
mean3 = fig.contour(lons,lats,day3[0,:,:],levels=60,extend='both',transform=fcst_crs)
mean4 = fig.contour(lons,lats,day4[0,:,:],levels=60,extend='both',transform=fcst_crs)
mean5 = fig.contour(lons,lats,day5[0,:,:],levels=60,extend='both',transform=fcst_crs)
mean6 = fig.contour(lons,lats,day6[0,:,:],levels=60,extend='both',transform=fcst_crs)
fig1 = plt.contour(mean1, colors = 'black')
fig2 = plt.contour(mean2, colors = 'black')
fig3 = plt.contour(mean3, colors = 'black')
fig4 = plt.contour(mean4, colors = 'black')
fig5 = plt.contour(mean5, colors = 'black')
fig6 = plt.contour(mean6, colors = 'black')
plt.savefig('500mbHGT.png')
plt.show(fig1)
plt.show(fig2)
plt.show(fig3)
plt.show(fig4)
plt.show(fig5)
plt.show(fig6)
When I run the above code, all of my data plots on to one map. I have tried to make several adjustments based on the documentation provided here: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.subplots.html
I've also tried following multiple examples of code using subplots for different uses. I am still unable to get separate maps plotted, but from what I understand, subplots is the way I should approach this problem, so hopefully I am on the right track.
By generating the subplots at the beginning with plt.subplots, for example:
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
fig, axes = plt.subplots(2, 1)
axes[0].set_title("Hammer projection")
map = Basemap(projection='hammer', lon_0 = 10, lat_0 = 50, ax=axes[0])
map.drawmapboundary(fill_color='aqua')
map.fillcontinents(color='coral',lake_color='aqua')
map.drawcoastlines()
axes[1].set_title("Robinson projection")
map = Basemap(projection='robin', lon_0 = 10, lat_0 = 50, ax=axes[1])
map.drawmapboundary(fill_color='aqua')
map.fillcontinents(color='coral',lake_color='aqua')
map.drawcoastlines()
plt.show()
Related
I've been attempting to limit the range on the colorbar function in matplotlib. For whatever reason, I cannot use the clim function. Ideally I would like 80 and 20 to be the max values of the colorbar, and all values above or below those values to be a single dark blue/red, and the entire colorbar to be fit within the range of 20 and 80.
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
import matplotlib as mpl
import numpy as np
Gpercent=40
xGpercent = 60
SCFpercent = 55
CFpercent = 45
Analytics = ['GF%','xGF%','SCF%','CF%']
AnalyticsValues = [Gpercent,xGpercent,SCFpercent,CFpercent]
AnalyticsValues = [float(val) for val in AnalyticsValues]
data_height_normalized = [x / 100 for x in AnalyticsValues]
fig, ax = plt.subplots(figsize=(15, 4))
#my_cmap = plt.cm.get_cmap('RdBu')
my_cmap = plt.cm.get_cmap('coolwarm_r')
colors = my_cmap(data_height_normalized)
rects = ax.bar(Analytics, AnalyticsValues, color=colors)
sm = ScalarMappable(cmap=my_cmap, norm=plt.Normalize(0,100))
plt.ylim(0, 100)
cbar = plt.colorbar(sm)
plt.yticks(np.arange(0, 100.8, 10))
plt.title('bob' + (" On Ice 5v5 Impact"))
plt.xlabel('Analytical Metric')
plt.ylabel('%')
fig.patch.set_facecolor('xkcd:white')
plt.show()
The plot comes out as follows. I'd like the colorbar to be more defined in a shorter range, while still showing the % from 0-100
The intent of your question is to add an upper and lower limit to the color bar only. I would like to set the lower limit to 20 and the upper limit to 80. I will answer with the understanding that
The gist of the code is to create a new colormap from the defined colormap using LinearSegmentedColormap with the upper and lower color range.
My answer was modified from this excellent answer to fit your assignment.
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
from matplotlib.colors import LinearSegmentedColormap # add
import matplotlib as mpl
import numpy as np
Gpercent=40
xGpercent = 60
SCFpercent = 55
CFpercent = 45
Analytics = ['GF%','xGF%','SCF%','CF%']
AnalyticsValues = [Gpercent,xGpercent,SCFpercent,CFpercent]
AnalyticsValues = [float(val) for val in AnalyticsValues]
data_height_normalized = [x / 100 for x in AnalyticsValues]
fig, ax = plt.subplots(figsize=(15, 4))
#my_cmap = plt.cm.get_cmap('RdBu')
my_cmap = plt.cm.get_cmap('coolwarm_r')
colors = my_cmap(data_height_normalized)
rects = ax.bar(Analytics, AnalyticsValues, color=colors)
# update
vmin,vmax = 20,80
colors2 = my_cmap(np.linspace(1.-(vmax-vmin)/float(vmax), 1, my_cmap.N))
color_map = LinearSegmentedColormap.from_list('cut_coolwarm', colors2)
sm = ScalarMappable(cmap=color_map, norm=plt.Normalize(vmin, vmax))
plt.ylim(0, 100)
cbar = plt.colorbar(sm)
plt.yticks(np.arange(0, 100.8, 10))
plt.title('bob' + (" On Ice 5v5 Impact"))
plt.xlabel('Analytical Metric')
plt.ylabel('%')
fig.patch.set_facecolor('xkcd:white')
plt.show()
I'm trying to plot GOES-East full disk data using metpy, and Siphon to download the latest data from the THREDDS data server. However, after comparing my plots with the realtime imagery, ther seems to be a large difference.
Below is my code:
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import metpy.calc as mpcalc
from metpy.plots.ctables import registry
from metpy.plots import add_timestamp
from metpy.units import units
from siphon.catalog import TDSCatalog
import xarray as xr
import numpy as np
from xarray.backends import NetCDF4DataStore
from datetime import datetime, timedelta
dt = datetime.utcnow().date()
data = TDSCatalog(f'http://thredds.ucar.edu/thredds/catalog/satellite/goes/east/products/'
f'CloudAndMoistureImagery/FullDisk/Channel09/{dt:%Y%m%d}/catalog.xml')
sat_dataset = data.datasets[0].remote_access(use_xarray = True)
cmi = sat_dataset.metpy.parse_cf('Sectorized_CMI')
x = cmi.coords['x'][:]
y = cmi.coords['y'][:]
timestamp = datetime.strptime(str(cmi.time.values.astype('datetime64[s]')), '%Y-%m-%dT%H:%M:%S')
print(timestamp)
vtime = timestamp.strftime('%Y-%m-%d %H%M%S')
# Create the figure
fig = plt.figure(figsize = [16, 10])
ax = fig.add_subplot(1, 1, 1, projection = cmi.metpy.cartopy_crs)
ax.set_extent([-80, -45, -50, -15], crs = ccrs.PlateCarree())
ax.add_feature(cfeature.BORDERS.with_scale('50m'), edgecolor = 'black', linewidth = 1)
ax.add_feature(cfeature.COASTLINE.with_scale('50m'), edgecolor = 'black', linewidth = 1)
ax.add_feature(cfeature.STATES.with_scale('50m'), edgecolor = 'white', linewidth = 1)
# Add mapping information
ax.add_feature(cfeature.STATES)
ax.add_feature(cfeature.BORDERS, linewidth=2)
# Plot the image with our colormapping choices
wv_norm, wv_cmap = registry.get_with_range('WVCIMSS_r', 193, 283)
im = ax.imshow(cmi, extent=(x[0], x[-1], y[0], y[-1]), origin='upper',
cmap = wv_cmap, norm = wv_norm, transform = cmi.metpy.cartopy_crs)
plt.colorbar(im, ticks = np.arange(193, 293, 10), ax = ax)
plt.title(f'Vapor da Água em Níveis Médios [$K$] \nValid: {vtime} UTC', loc = 'left')
plt.savefig(f'/mnt/c/Users/vitor/Desktop/WV_{vtime}.jpg', bbox_inches = 'tight')
Also below, is a comparison between the output from my code and the actual water vapor imagery from the CODNEXLAB website. I also looked at the metadata of the downloaded files and everything seems to be fine. Not sure if I'm doing something wrong here.
What you're seeing is that your image is flipped (it's easier to identify if you look at the global plot of that data). What's happening is the origin you specified ('upper'/'lower') disagree with what you passed as extent. So either tweak your origin parameter:
im = ax.imshow(cmi, extent=(x[0], x[-1], y[0], y[-1]),
origin='lower', cmap=wv_cmap, norm=wv_norm,
transform=cmi.metpy.cartopy_crs)
or flip the order of your y extents:
im = ax.imshow(cmi, extent=(x[0], x[-1], y[-1], y[0]),
origin='upper', cmap=wv_cmap, norm=wv_norm,
transform=cmi.metpy.cartopy_crs)
I'm making plots using matplotlib colormap "seismic" and would like to have the white color centered on 0. When I run my script with no changes, white falls from 0 to -10. I tried then setting vmin=-50, vmax=50 but I completely lose the white in that case. Any suggestions on how to accomplish that?
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
nc = NetCDFFile('myfile.nc')
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time = nc.variables['time'][:]
hgt = nc.variables['hgt'][:]
map = Basemap(llcrnrlon=180.,llcrnrlat=0.,urcrnrlon=320.,urcrnrlat=80.)
lons,lats = np.meshgrid(lon,lat)
x,y = map(lons,lats)
cs = map.contourf(x,y,hgt[0],cmap='seismic')
cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5,
cmap='seismic')
cbar.set_label('500mb Geopotential Height Anomalies(m)')
map.drawcoastlines()
map.drawparallels(np.arange(20,80,20),labels=[1,1,0,0], linewidth=0.5)
map.drawmeridians(np.arange(200,320,20),labels=[0,0,0,1], linewidth=0.5)
plt.show()`
Plot with defaults
Plot with vmin, vmax set
You can set the levels you want to show manually. As long as you have the same spacing of intervals to the left and to the right of zero this works nicely.
levels = [-50,-40,-30,-20,-10,10,20,30,40,50]
ax.contourf(X,Y,Z, levels)
Example:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-6.3,6.3)
y = np.linspace(-3.1,3.1)
X,Y = np.meshgrid(x,y)
Z = -np.cos(X)*np.cos(Y)*45
levels = [-50,-40,-30,-20,-10,10,20,30,40,50]
fig, ax = plt.subplots(figsize=(4,2))
cont = ax.contourf(X,Y,Z,levels, cmap="seismic")
fig.colorbar(cont, orientation="horizontal")
plt.show()
Or, if you want the colorbar to be proportional to the data,
fig.colorbar(cont, orientation="horizontal", spacing="proportional")
If levels are unequal, you need to specify vmin and vmax.
levels = [-50,-40,-30,-20,-10,10,30,50,80,100]
cont = ax.contourf(X,Y,Z,levels, cmap="seismic", vmin=-50, vmax=50)
The disadvantage is that you loose resolution, hence you may use a BoundaryNorm to select equally spaced colors for unequally spaced labels.
import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np
x = np.linspace(-6.3,6.3)
y = np.linspace(-3.1,3.1)
X,Y = np.meshgrid(x,y)
Z = -np.cos(X)*np.cos(Y)*45
levels = [-50,-40,-30,-20,-10,10,30,50,80,100]
norm = matplotlib.colors.BoundaryNorm(levels, len(levels)-1)
fig, ax = plt.subplots(figsize=(4,2))
cont = ax.contourf(X,Y,Z,levels,cmap=plt.get_cmap("seismic",len(levels)-1), norm=norm)
fig.colorbar(cont, orientation="horizontal")
plt.show()
To change the ticklabels on the colorbar so something other than the levels or in case they are too dence you may use the ticks argument.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-6.3,6.3)
y = np.linspace(-3.1,3.1)
X,Y = np.meshgrid(x,y)
Z = -np.cos(X)*np.cos(Y)*45
levels = np.arange(-45,50,5)
levels = levels[levels!=0]
ticks=np.arange(-40,50,10)
fig, ax = plt.subplots(figsize=(4,2))
cont = ax.contourf(X,Y,Z,levels,cmap="seismic", spacing="proportional")
fig.colorbar(cont, orientation="horizontal", ticks=ticks, spacing="proportional")
plt.show()
In the following code, the color of bars changes as the threshold is changed. Instead of using the threshold and plotting the horizontal line in the code, I want to use the y parameter in the OnMouseMove function so that the user can change the location of "threshold". Then, I want the colors to be updated as the y is changed.
I think what I need is called "observer pattern" or perhaps a trick using the animation tools but not sure how to implement it. I appreciate any insight on how to do this. Thanks
%matplotlib notebook
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.colors as mcol
import matplotlib.cm as cm
import matplotlib.pyplot as plt
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(335,1500,300),
np.random.normal(410,900,300),
np.random.normal(410,1200,300),
np.random.normal(480,550,300)],
index=[1,2,3,4])
fig, ax = plt.subplots()
plt.show()
bars = plt.bar(range(df.shape[0]), df.mean(axis = 1), color = 'lightslategrey')
fig = plt.gcf()
threshold=420
plt.axhline(y = threshold, color = 'grey', alpha = 0.5)
cm1 = mcol.LinearSegmentedColormap.from_list("Test",["b", "white", "purple"])
cpick = cm.ScalarMappable(cmap=cm1)
cpick.set_array([])
percentages = []
for bar in bars:
percentage = (bar.get_height()-threshold)/bar.get_height()
if percentage>1: percentage = 1
if percentage<0: percentage=0
percentages.append(percentage)
cpick.to_rgba(percentages)
bars = plt.bar(range(df.shape[0]), df.mean(axis = 1), color = cpick.to_rgba(percentages))
plt.colorbar(cpick, orientation='horizontal')
def onMouseMove(event):
ax.lines = [ax.lines[0]]
plt.axhline(y=event.ydata, color="k")
fig.canvas.mpl_connect('motion_notify_event', onMouseMove)
plt.xticks(range(df.shape[0]), df.index, alpha = 0.8)
First you should use exactly one bar plot and exactly one axhline (using more will make everything chaotic). You can set the colors of the bars via
for bar in bars:
bar.set_color(..)
and you can update the axhline's position via line.set_ydata(position).
Now, for every mouse move event you need to update the axhline's position, calculate the percentages and apply a new colors to the bars. So those things should be done in a function, which is called every time the mouse move event is triggered. After those settings have been applied the canvas needs to be drawn for them to become visible.
Here is a complete code.
import pandas as pd
import numpy as np
import matplotlib.colors as mcol
import matplotlib.cm as cm
import matplotlib.pyplot as plt
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(335,1500,300),
np.random.normal(410,900,300),
np.random.normal(410,1200,300),
np.random.normal(480,550,300)],
index=[1,2,3,4])
fig, ax = plt.subplots()
threshold=420.
bars = plt.bar(range(df.shape[0]), df.mean(axis = 1), color = 'lightslategrey')
axline = plt.axhline(y = threshold, color = 'grey', alpha = 0.5)
cm1 = mcol.LinearSegmentedColormap.from_list("Test",["b", "white", "purple"])
cpick = cm.ScalarMappable(cmap=cm1)
cpick.set_array([])
plt.colorbar(cpick, orientation='horizontal')
def percentages(threshold):
percentages = []
for bar in bars:
percentage = (bar.get_height()-threshold)/bar.get_height()
if percentage>1: percentage = 1
if percentage<0: percentage=0
percentages.append(percentage)
return percentages
def update(threshold):
axline.set_ydata(threshold)
perc = percentages(threshold)
for bar, p in zip(bars, perc):
bar.set_color(cpick.to_rgba(p))
# update once before showing
update(threshold)
def onMouseMove(event):
if event.inaxes == ax:
update(event.ydata)
fig.canvas.draw_idle()
fig.canvas.mpl_connect('motion_notify_event', onMouseMove)
plt.xticks(range(df.shape[0]), df.index, alpha = 0.8)
plt.show()
I cannot add a colorbar to my 3D scatter plot that is coloured in range of min and max according to the value of bifurWidth. I've tried various attempts shown on stackoverflow, none have had any success. Any help would really be appreciated, as I am at a major loss with this.
My most recent attempt is hashed out of the code shown below.
My code:
from glob import glob
from pylab import *
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)
fig = figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = get_cmap("jet")
fig.tight_layout()
fig.set_size_inches(25,15)
min = 3 #colorbar range
max = 10
lw = 0 #linewidth
s = 10 #scatter size
for idx, p in enumerate(dataSorted[:,1]):
powerLoop = dataSorted[idx,0]
powerLoop = powerLoop.astype(np.float)
bifurWidthLoop = dataSorted[idx,2]
bifurWidthLoop = bifurWidthLoop.astype(np.float)
y0 = genfromtxt(p, unpack=True, usecols=[0], skiprows=19, skip_footer=1)
length = len(x0)
power_array = [powerLoop] * length
bifurWidth_array = [bifurWidthLoop] * length
label = str(bifurWidth)
a = myScatter(x0,power_array,y0,bifurWidth_array,lw,s,min,max,cmhot,label,ax)
#cax = ax.imshow(y0, interpolation='nearest', vmin=min, vmax=max)
#fig.colorbar(cax)
fig.savefig('test.png',dpi=300)
Example of an attempt and its error:
If I use fig.colorbar(a) inside or outside of the plotting for loop, I return NoneType oject has no attribute autoscale_None.
Your code doesn't run (x0,dataSorted,y0,etc missing) so can't get it to work (also note x0,power_array,y0 are wrong order in fn call). You need to return the handle to the scatter plot in order to plot a colorbar. If you change your myScatter function to return the handle,
def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
return ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)
and then call plt.colorbar(a). A minimal(ish) example would be,
from glob import glob
from pylab import *
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
return ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)
fig = figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = get_cmap("jet")
fig.tight_layout()
fig.set_size_inches(25,15)
min = 3 #colorbar range
max = 10
lw = 0 #linewidth
s = 10 #scatter size
label = 'test'
power_array = np.random.random((100,10))
bifurWidth_array = np.random.random((100,10))*(max-min)+min
x0 = np.random.random((100,10))
y0 = np.random.random((100,10))
a = myScatter(x0,power_array,y0,bifurWidth_array,lw,s,min,max,cmhot,label,ax)
plt.colorbar(a)
plt.show()