Change colour scale increments in Python - python

I have created a frequency time spectrogram plot seen below.
I want to edit the colour scale so that the higher frequencies shown from 20 seconds are more prominent. I think having smaller increments at the lower end of the colour scale (blues) would achieve this but am not sure how to do it. Any help would be great!
Here is what I have so far:
import numpy as np
import matplotlib.pyplot as plt
from obspy.core import read
from obspy.signal.tf_misfit import cwt
import pylab
tr = read("whole.sac")[0]
npts = tr.stats.npts
dt = tr.stats.delta
t = np.linspace(0, dt * npts, npts)
f_min = 1
f_max = 10
scalogram = cwt(tr.data, dt, 8, f_min, f_max)
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.7, 0.60])
ax2 = fig.add_axes([0.1, 0.75, 0.75, 0.2])
ax3 = fig.add_axes([0.83, 0.1, 0.03, 0.6])
img = ax1.imshow(np.abs(scalogram)[-1::-1], extent=[t[0], t[-1], f_min, f_max],
aspect='auto', interpolation="nearest")
ax1.set_xlabel("Time after %s [s]" % tr.stats.starttime)
ax1.set_ylabel("Frequency [Hz]")
ax1.set_yscale('linear')
ax2.plot(t, tr.data, 'k')
pylab.xlim([30,72])
fig.colorbar(img, cax=ax3)
plt.show()

You could try other colormaps or make you own according to this recipe.
Or you may want to filter the data to set all values above a given threshold (e.g. 60) to the threshold value. This would use the entire range of the colormap on the range of interest. You can easily use np.clip() to do this.
So...
np.abs(scalogram)[-1::-1]
becomes
np.clip(np.abs(scalogram)[-1::-1], 0, 100)
to clip between 0 and 100.

Related

Use of "extend" in a contourf plot with a discrete colorbar not working

I would like to create a contourf plot with an imposed maximum value and with everything above that value shaded with the last color of the colorbar. In the example code below, which reproduces my problem in my setup, I would like the colorbar to range between -1 and 1, with an extend arrow indicating that values above 1.0 will be shaded with the last color of the colorbar. However, although I have tried several solutions from various stackexchange discussions, the colorbar ranges between -4 and 4, and there is no extend arrow. Please see the minimum reproducible example below.
# import matplotlib (v 3.1.1)
import matplotlib.colors as colors
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib as mpl
# import numpy (v 1.17.2)
import numpy as np
# define grid
lon = np.linspace(start = 0, stop = 359, num = 360)
lat = np.linspace(start = -78, stop = -25, num = 52)
[X,Y] = np.meshgrid(lon, lat)
# generate random gaussian data for example purposes
mean = [0, 0]
cov = [[1, 0], [0, 100]]
zz = np.random.multivariate_normal(mean, cov, (np.size(lon),np.size(lat))).T
Z = zz[0,:,:]
# illutrate the maximum value of Z
np.max(Z)
# create plot
plt.figure(figsize=(10, 12))
# select plotting levels (missing min/max on purpose)
mylevs = [-1.0, -0.5, 0, 0.5, 1.0]
# colormap
cmap_cividis = plt.cm.get_cmap('cividis',len(mylevs))
mycolors = list(cmap_cividis(np.arange(len(mylevs))))
cmap = colors.ListedColormap(mycolors[:-1], "")
# set over-color to last color of list
cmap.set_over(mycolors[-1])
# contour plot: random pattern
C1 = plt.contourf(X, Y, Z, cmap = cmap, vmin=-1.0, vmax=1.0,
norm = colors.BoundaryNorm(mylevs, ncolors=len(mylevs)-1, clip=False))
# create colorbar
cbar = plt.colorbar(C1, orientation="horizontal", extend='max')
cbar.ax.tick_params(labelsize=20)
cbar.set_label('Random field', size='xx-large')
I would like the colorbar to stop at 1.0, with an extend arrow pointing to the right, shaded by the last color of the colorbar. Thanks in advance for any help you can provide.
Link to example image produced by the above code
Does this solve it?
fig,ax = plt.subplots()
mylevs = [-1.0, -0.5, 0, 0.5, 1.0]
C1 = ax.contourf(X, Y, Z, cmap = cmap, vmin=-1.0, vmax=1.0,levels=mylevs,extend='both')
fig.colorbar(C1)

Using brewer2mpl diverging colormap with matplotlib, gamma gives poor results with values different than 1

Playing with colormaps in this question I was introduced to brewer2mpl in comments. For the diverging map (with zero in the middle) I noticed that gamma is implemented to work in a naive way and so gives unexpected† results when it is not equal to 1. The neutral tone meant to represent zero slides around so that values near zero are blue with gamma = 0.5 and orang when gamma = 1.5
Is there anyway to make a gamma-like parameter that operates symmetrically around zero in matplotlib or brewer2mpl or do I just have to do it manually somehow by raising np.abs(u) to some power then re-normalizing and re-introducing the sign?
†expected behavior would be to apply gamma symmetrically about zero; at least that's what I had expected.
import numpy as np
import matplotlib.pyplot as plt
import brewer2mpl
Re = 6378137. # meters
J2 = 1.7555E+25 # m^5/s^2
hw = 400
x = np.linspace(-2.5*Re, 2.5*Re, 2*hw+1)
x, z = np.meshgrid(x, x)
r = np.sqrt(x**2 + z**2)
u = J2 * r**-5 * 0.5 * (3*z**2 - r**2)
u[r<Re] = np.nan
if True:
umax = np.nanmax(np.abs(u))
bmap = brewer2mpl.get_map('RdBu', 'Diverging', 9)
gammas = 0.5, 1, 1.5
plt.figure()
for i, gamma in enumerate(gammas):
cmap = bmap.get_mpl_colormap(N=100, gamma=gamma)
plt.subplot(1, 3, i+1)
plt.imshow(u, cmap=cmap, vmin=-umax, vmax=umax)
plt.colorbar()
plt.title('gamma = ' + str(gamma))
plt.plot([hw, hw], [0.3*hw, 1.7*hw], '-k')
plt.xlim(0, 2*hw+1)
plt.ylim(0, 2*hw+1)
plt.suptitle("Earth's geopotential's J2 component", fontsize=16)
plt.show()
You can create two colormaps, one from red to white, one from white to blue. Then apply gamma to each of those. Finally get the colors from those colormaps and create a new one with the combined colors.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from copy import copy
cmap = plt.get_cmap("RdBu", lut=256)
# Create two new colormaps, each with one half of the original
cmap_lower = LinearSegmentedColormap.from_list("", cmap(np.arange(0,128)))
cmap_upper = LinearSegmentedColormap.from_list("", cmap(np.arange(128,256)))
gammas = [1, 0.5, 1.5]
fig, axs = plt.subplots(ncols=3, figsize=(8,5))
for ax, gamma in zip(axs, gammas):
# copy each colormap and set the respective gamma
cm1 = copy(cmap_lower)
cm1.set_gamma(gamma)
cm2 = copy(cmap_upper)
cm2.set_gamma(gamma)
# get the colors from the each
colors = np.concatenate((cm1(np.arange(0,256)), cm2(np.arange(0,256))), axis=0)
this_cmap = LinearSegmentedColormap.from_list("", colors)
im = ax.imshow(np.arange(300).reshape(30,10), cmap=this_cmap)
fig.colorbar(im, ax=ax)
ax.set_title(f"gamma={gamma}")
plt.show()

Seaborn distplot y-axis normalisation wrong ticklabels

Just to note, I have already checked this question and this question.
So, I'm using distplot to draw some histograms on separate subplots:
import numpy as np
#import netCDF4 as nc # used to get p0_dict
import matplotlib.pyplot as plt
from collections import OrderedDict
import seaborn.apionly as sns
import cPickle as pickle
'''
LINK TO PICKLE
https://drive.google.com/file/d/0B8Xks3meeDq0aTFYcTZEZGFFVk0/view?usp=sharing
'''
p0_dict = pickle.load(open('/path/to/pickle/test.dat', 'r'))
fig = plt.figure(figsize = (15,10))
ax = plt.gca()
j=1
for region, val in p0_dict.iteritems():
val = np.asarray(val)
subax = plt.subplot(5,5,j)
print region
try:
sns.distplot(val, bins=11, hist=True, kde=True, rug=True,
ax = subax, color = 'k', norm_hist=True)
except Exception as Ex:
print Ex
subax.set_title(region)
subax.set_xlim(0, 1) # the data varies from 0 to 1
j+=1
plt.subplots_adjust(left = 0.06, right = 0.99, bottom = 0.07,
top = 0.92, wspace = 0.14, hspace = 0.6)
fig.text(0.5, 0.02, r'$ P(W) = 0,1 $', ha ='center', fontsize = 15)
fig.text(0.02, 0.5, '% occurrence', ha ='center',
rotation='vertical', fontsize = 15)
# obviously I'd multiply the fractional ticklabels by 100 to get
# the percentage...
plt.show()
What I expect is for the area under the KDE curve to sum to 1, and for the y axis ticklabels to reflect this. However, I get the following:
As you can see, the y axis ticklabels are not in the range [0,1], as would be expected. Turning on/off norm_hist or kde does not change this. For reference, the output with both turned off:
Just to verify:
aus = np.asarray(p0_dict['AUS'])
aus_bins = np.histogram(aus, bins=11)[0]
plt.subplot(121)
plt.hist(aus,11)
plt.subplot(122)
plt.bar(range(0,11),aus_bins.astype(np.float)/np.sum(aus_bins))
plt.show()
The y ticklabels in this case properly reflect those of a normalised histogram.
What am I doing wrong?
Thank you for your help.
The y axis is a density, not a probability. I think you are expecting the normalized histogram to show a probability mass function, where the sum the bar heights equals 1. But that's wrong; the normalization ensures that the sum of the bar heights times the bar widths equals 1. This is what ensures that the normalized histogram is comparable to the kernel density estimate, which is normalized so that the area under the curve is equal to 1.

Matplotlib Colorbar Display Digtis

How do I exactly specify the colorbar labels in matplotlib? Frequently, I need to create very specific color scales, but the colorbar labels display so poorly you can't tell what the scale is. I would like to manually define the text next to the colorbar tick marks, or at least have them display in scientific notation.
Here is an example plot where you can't tell what the bottom four color bins represent:
And here is a working example of how that plot was created:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
# mock up some data
x = np.random.random(50)
y = np.random.random(50)
c = np.arange(0, 1, 1.0/50.0) # color of points
c[0] = 0.00001
c[1] = 0.0001
c[2] = 0.001
c[3] = 0.01
s = 500 * np.random.random(50) + 25 # size of points
# set up some custom color scaling
lcmap = colors.ListedColormap(['#FFFFFF', '#FF99FF', '#8000FF',
'#0000FF', '#0080FF', '#58FAF4',
'#00FF00', '#FFFF00', '#FF8000',
'#FF0000'])
bounds = [0.0, 0.000001, 0.00001, 0.0001,
0.001, 0.01, 0.1, 0.25, 0.5, 0.75, 1.0]
norm = colors.BoundaryNorm(bounds, lcmap.N)
# create some plot
fig, ax = plt.subplots()
im = ax.scatter(x, y, c=c, s=s, cmap=lcmap, norm=norm)
# add the colorbar
fig.colorbar(im, ax=ax)
fig.savefig('temp.jpg')
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_xticklabels(['Low', 'Medium', 'High'])
and use whatever iterable you want instead of ['Low', 'Medium', 'High']
see: http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html

Matplotlib subplot y-axis scale overlaps with plot above

I am trying to plot 3 subplots without any white space between them. The default y axis ticklabels use a scale displayed to the top right of the y axis (1e-8 in the example below), which would be fine except for the lower two plots this overlaps with the plot above. Anyone know how to fix this? A small example is below.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator
x = np.arange(0,200)
y = np.random.rand(200) * 10e-8
fig = plt.figure(figsize=(10,15))
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0)
ax0a = plt.subplot(gs1[0, :])
ax0b = plt.subplot(gs1[1, :])
ax0c = plt.subplot(gs1[2, :])
ax0a.set_xticklabels([])
ax0b.set_xticklabels([])
ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
so one solution is to use mtick,
import matplotlib.ticker as mtick
ax0a.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
ax0b.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
ax0c.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
but I would prefer to be able to shift the scale to the left so it is outside the axis if possible.
I have two options you might want to look at.
First, set the axis location and size yourself as such:
# your imports and data above
fig = plt.figure()
ax0a = fig.add_axes([0.1, 0.1, 0.8, 0.25])
ax0b = fig.add_axes([0.1, 0.39, 0.8, 0.25], sharex=ax0a)
ax0c = fig.add_axes([0.1, 0.68, 0.8, 0.25], sharex=ax0a)
ax0a.set_xticklabels([])
ax0b.set_xticklabels([])
ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
plt.show()
The second option is to manually adjust the location and maybe font size of the offset text:
# your original code minus data and imports
fig = plt.figure()
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0)
ax0a = plt.subplot(gs1[0, :])
ax0b = plt.subplot(gs1[1, :])
ax0c = plt.subplot(gs1[2, :])
ax0a.set_xticklabels([])
ax0b.set_xticklabels([])
ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
# play around with location and font of offset text here
ax0a.get_yaxis().get_offset_text().set_x(-0.075)
ax0a.get_yaxis().get_offset_text().set_size(10)
ax0b.get_yaxis().get_offset_text().set_x(-0.075)
ax0b.get_yaxis().get_offset_text().set_size(10)
ax0c.get_yaxis().get_offset_text().set_x(-0.075)
ax0c.get_yaxis().get_offset_text().set_size(10)
plt.show()
Ok, this is probably an ugly solution but you could just simply upscale
your data in the lower plots with the range of the power i.e. ax0b.plot(x, y*1e8).
This works for your example at least.

Categories