I am trying to do a matplotlib contourf plot with some x, y, and z values. Basically the z values will define the color of the plot.
However, where I am at right now one region (i.e. the important region for me) is very small compared to the rest (see figure), so it can be very difficult to see this particular region actually (a few small black "dots"). So I was thinking if it was possible maybe to get the first lvl (or last level since it's negative values in this case) in another color, or maybe outline it with a thin white line or something, so one can really see the small and important dots ?
I am plotting with this code:
import matplotlib.pyplot as plt
from matplotlib import rcParams
import matplotlib.colors as colors
import numpy as np
nx = 41
ny = 67
x = np.linspace(0.01, 1, nx)
y = np.linspace(0.01, 2, ny)
x_bc = x[:, np.newaxis]
y_bc = y[np.newaxis, :]
z = x_bc*y_bc
max_value = np.amax(z)
cmapp = plt.get_cmap('Greys')
level_intervals = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 8, 1.92, 0]
level_list = [max_value-i for i in level_intervals]
col_bar = plt.contourf(x, y, z.T, level_list, cmap=cmapp)
plt.xlabel('x')
plt.ylabel('y')
plt.colorbar(col_bar, cmap=cmapp)
plt.show()
I am sorry for not providing any real data, but I can't replicate the data used for the plot below (where there actually is some small amounts/dots of almost black, inside the almost black (weird sentence). However, the size and how the z data is created is just as above. There has, however, been many calculations in between before getting the data from the figure.

You can create a custom colormap based on an existing one and replace one of the colors with e.g. red.
You may then use a BoundaryNorm to use the colors from the new colormap for the specified levels.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
d = np.linspace(-3,3)
x,y = np.meshgrid(d,d)
data = -585.22 + 94*np.exp(-(x**2+y**2))
levels = np.linspace(-585.22, -485.22, 13)
norm = matplotlib.colors.BoundaryNorm(levels,len(levels))
colors = list(plt.cm.Greys(np.linspace(0,1,len(levels)-1)))
colors[-1] = "red"
cmap = matplotlib.colors.ListedColormap(colors,"", len(colors))
im = plt.contourf(data, levels, cmap=cmap, norm=norm)
plt.colorbar(ticks=levels)
plt.show()

Edit based on your comment below: You can restrict the contours in the region/range you want. For example, I modified the x, y, and z data in your sample code above to plot more contour lines. I then select only the contour lines for highest magnitude levels = sorted(level_list)[-5:] (last 5 lines here) for highlighting with the red color. Try doing it for your actual data and see if the points in the region of interest become visible. I am writing below only the lines which I modified in your code.
fig = plt.figure(figsize=(8, 6))
nx = 67
ny = 77
# Modified your actual values to get some more contour lines
x = np.linspace(1, 16, nx)
y = np.linspace(1, 15, ny)
z = x_bc*y_bc*0.2
col_bar = plt.contourf(x, y, z.T, level_list, cmap=cmapp)
plt.contour(col_bar, levels = sorted(level_list)[-5:], colors=('r',),linestyles=('-',),linewidths=(3,))
Output

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With