'plt.contourf' with given number of levels in logscale - python

With these lines, currently I'm having this kind of figure.
fig, ax = plt.subplots()
X, Y = np.meshgrid(x, y)
cs = ax.contourf(X, Y, Z, 50, cmap=cm.get_cmap('jet')) # linear mapping
#cs = ax.contourf(X, Y, Z, 50, locator=ticker.LogLocator(), cmap=cm.get_cmap('jet')) # log mapping
cbar = fig.colorbar(cs)
plt.show()
Now I want to plot this in Log scale, so if I activate the commented line, I get this kind of result which seems to ignore 'levels' argument which is set to '50'.
I've reached this post (Python matplotlib contour plot logarithmic color scale), but I am pretty sure that there is a way in which I do not have to set all the values of levels manually.
Does anyone has a comment, or any other handy python functions for logarithmatic contour plot with many levels?

Setting the number of levels as a integer doesn't work for logscale but you can easily set the values with np.logspace(np.log10(z.min()),np.log10(z.max()), 50). Matplotlib 3.3.3 seems to have some difficulties in correctly formatting the colorbar ticks in this case, so you need to manually adjust them a bit.
import matplotlib.pyplot as plt
from matplotlib import ticker, cm
import numpy as np
x = np.linspace(-3.0, 3.0, 100)
y = np.linspace(-2.0, 2.0, 100)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X)**2 - (Y)**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
z = Z1 + 50 * Z2
fig, ax = plt.subplots()
n_levels = 50
cs = ax.contourf(X, Y, z,
np.logspace(np.log10(z.min()),np.log10(z.max()), n_levels),
locator=ticker.LogLocator(),
cmap=cm.jet
)
cbar = fig.colorbar(cs)
cbar.locator = ticker.LogLocator(10)
cbar.set_ticks(cbar.locator.tick_values(z.min(), z.max()))
cbar.minorticks_off()

Related

3D surface plot with section plan in Matplotlib

Basically I have a surface plot consisting of a set of time series, and I would like to add a section plan at a specific height, to better understand the period of the year when values are higher than the selected threshold.
From this:
where the plan is shown but not as a section
To This:
Any suggestion?
Plying with alpha and camera elevation did not solve the issue
the plan still seems to be in front of the figure, not as a section
Drawing in 3 steps
As others pointed out, matplotlib's 3D capabilities are somewhat limited. To hide objects behind other objects, it uses the painter's algorithm. So, the objects are simply drawn back to front, and no objects are put partly before and partly behind some plane. Matplotlib calculates some average depth of each object to define the order. You can overwrite this order via ax.computed_zorder = False, as the automatic calculation is not always what is wished.
You could draw the "layers" yourself:
the 3D surface
then the plane
then the part of the 3D surface that should be visible on top
An example:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.ndimage.filters import gaussian_filter
x = np.linspace(-10, 10, 51)
y = np.linspace(-10, 10, 51)
X, Y = np.meshgrid(x, y)
np.random.seed(20220201)
Z = np.random.rand(*X.shape) ** 5
Z[X ** 2 + Y ** 2 > 30] = 0
Z = gaussian_filter(Z, sigma=2) * 100
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.computed_zorder = False
ax.plot_surface(X, Y, Z, cmap='turbo')
special_z = 16
ax.plot_surface(X, Y, np.full_like(Z, special_z), color='blue', alpha=0.4)
ax.plot_surface(X, Y, np.where(Z >= special_z, Z, np.nan), cmap='turbo', vmin=0)
plt.show()
Drawing layer by layer
An alternative way could be to draw the surface one layer at a time.
The example at the left shows the surface divided into 30 layers, the example at the right stops at a given height, visualizing the intersection.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.ndimage.filters import gaussian_filter
x = np.linspace(-10, 10, 51)
y = np.linspace(-10, 10, 51)
X, Y = np.meshgrid(x, y)
np.random.seed(20220201)
Z = np.random.rand(*X.shape) ** 5
Z[X ** 2 + Y ** 2 > 30] = 0
Z = gaussian_filter(Z, sigma=2) * 100
fig = plt.figure()
for which in ['left', 'right']:
ax = fig.add_subplot(121 + (which == 'right'), projection="3d")
ax.computed_zorder = False
layers = np.linspace(Z.min(), Z.max(), 32)[1:-1]
colors = plt.get_cmap('turbo', len(layers)).colors
special_z = 16
plane_drawn = False
for layer, color in zip(layers, colors):
if layer >= special_z and not plane_drawn:
ax.plot_surface(X, Y, np.full_like(Z, special_z), color='blue', alpha=0.5, zorder=2)
plane_drawn = True
ax.contour(X, Y, Z, levels=[layer], offset=layer, colors=[color])
if plane_drawn and which == 'right':
break
plt.show()

Ticks in colorbar of matplotlib (Python) are not positioned correctly when I add an arbitrary value

I have plotted a contourf (and contour to plot lines on top) graph with colorbar and equispaced data levels and it is ok. However, when I add an arbitrary level value(1.1 in the example), it is not representd correctly in the colorbar.
In the reproducible example below, besides the levels with steps of 0.5, a new value with a step of 0.1 is added. In the colorbar, the distances are the same.
How can I correct this?
Thanks in advance
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-2.0, 3.0, 0.025)
y = np.arange(-2.0, 3.0, 0.025)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
levels = np.arange(-2,3,1)
levels=np.append(levels,1.1)
levels=np.sort(levels)
fig, ax = plt.subplots()
c = plt.contourf(X, Y, Z, levels=levels,alpha=0.15)
cs = plt.contour(X, Y, Z, levels=levels)
ax.clabel(cs, inline=1, fontsize=10)
cbar=plt.colorbar(c)
plt.show()
You should pass spacing parameter to plt.colorbar:
spacing: uniform spacing gives each discrete color the same space; proportional makes the space proportional to the data interval.
cbar=plt.colorbar(c, spacing = 'proportional')

How to draw a scatter map using customized colors [duplicate]

An image is worth a thousand words :
https://www.harrisgeospatial.com/docs/html/images/colorbars.png
I want to obtain the same color bar than the one on the right with matplotlib.
Default behavior use the same color for "upper"/"lower" and adjacent cell...
Thank you for your help!
Here is the code I have:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
N = 100
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
# even bounds gives a contour-like effect
bounds = np.linspace(-1, 1, 10)
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
pcm = ax.pcolormesh(X, Y, Z,
norm=norm,
cmap='RdBu_r')
fig.colorbar(pcm, ax=ax, extend='both', orientation='vertical')
In order to have the "over"/"under"-color of a colormap take the first/last color of that map but still be different from the last color inside the colormapped range you can get one more color from a colormap than you have boundaries in the BoundaryNorm and use the first and last color as the respective colors for the "over"/"under"-color.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
N = 100
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
# even bounds gives a contour-like effect
bounds = np.linspace(-1, 1, 11)
# get one more color than bounds from colormap
colors = plt.get_cmap('RdBu_r')(np.linspace(0,1,len(bounds)+1))
# create colormap without the outmost colors
cmap = mcolors.ListedColormap(colors[1:-1])
# set upper/lower color
cmap.set_over(colors[-1])
cmap.set_under(colors[0])
# create norm from bounds
norm = mcolors.BoundaryNorm(boundaries=bounds, ncolors=len(bounds)-1)
pcm = ax.pcolormesh(X, Y, Z, norm=norm, cmap=cmap)
fig.colorbar(pcm, ax=ax, extend='both', orientation='vertical')
plt.show()
As suggested in my comment you can change the color map with
pcm = ax.pcolormesh(X, Y, Z, norm=norm, cmap='rainbow_r')
That gives:
You can define your own color map as shown here: Create own colormap using matplotlib and plot color scale

Contour graph in python

How would I make a countour grid in python using matplotlib.pyplot, where the grid is one colour where the z variable is below zero and another when z is equal to or larger than zero? I'm not very familiar with matplotlib so if anyone can give me a simple way of doing this, that would be great.
So far I have:
x= np.arange(0,361)
y= np.arange(0,91)
X,Y = np.meshgrid(x,y)
area = funcarea(L,D,H,W,X,Y) #L,D,H and W are all constants defined elsewhere.
plt.figure()
plt.contourf(X,Y,area)
plt.show()
You can do this using the levels keyword in contourf.
import numpy as np
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1,2)
x = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, x)
Z = np.sin(X)*np.sin(Y)
levels = np.linspace(-1, 1, 40)
zdata = np.sin(8*X)*np.sin(8*Y)
cs = axs[0].contourf(X, Y, zdata, levels=levels)
fig.colorbar(cs, ax=axs[0], format="%.2f")
cs = axs[1].contourf(X, Y, zdata, levels=[-1,0,1])
fig.colorbar(cs, ax=axs[1])
plt.show()
You can change the colors by choosing and different colormap; using vmin, vmax; etc.

Python/matplotlib mplot3d- how do I set a maximum value for the z-axis?

I am trying to make a 3-dimensional surface plot for the expression: z = y^2/x, for x in the interval [-2,2] and y in the interval [-1.4,1.4]. I also want the z-values to range from -4 to 4.
The problem is that when I'm viewing the finished surfaceplot, the z-axis values do not stop at [-4,4].
So my question is how I can "remove" the z-axis value that range outside the intervall [-4,4] from the finished plot?
My code is:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection="3d")
x = np.arange(-2.0,2.0,0.1,float) # x in interval [-2,2]
y = np.arange(-1.4,1.4,0.1,float) # y in interval [-1.4,1.4]
x,y = np.meshgrid(x,y)
z = (y**2/x) # z = y^2/x
ax.plot_surface(x, y, z,rstride=1, cstride=1, linewidth=0.25)
ax.set_zlim3d(-4, 4) # viewrange for z-axis should be [-4,4]
ax.set_ylim3d(-2, 2) # viewrange for y-axis should be [-2,2]
ax.set_xlim3d(-2, 2) # viewrange for x-axis should be [-2,2]
plt.show()
I am having the same issue and still have not found anything better than clipping my data. Unfortunately in my case I am tied to matplotlib 1.2.1. But in case you can upgrade to version 1.3.0 you could have a solution: it seems there is a bunch of new API related to axes ranges. In particular, you may be interested by the "set_zlim".
Edit 1: Manage to migrate my environnement to use matplotlib 1.3.0; set_zlim worked like a charm :)
The follwing code worked for me (By the way I am running this on OSX, I am not sure this has an impact?):
# ----------------------------------------------------------------------------
# Make a 3d plot according to data passed as arguments
def Plot3DMap( self, LabelX, XRange, LabelY, YRange, LabelZ, data3d ) :
fig = plt.figure()
ax = fig.add_subplot( 111, projection="3d" )
xs, ys = np.meshgrid( XRange, YRange )
surf = ax.plot_surface( xs, ys, data3d )
ax.set_xlabel( LabelX )
ax.set_ylabel( LabelY )
ax.set_zlabel( LabelZ )
ax.set_zlim(0, 100)
plt.show()
clipping your data will accomplish this, but it's not very pretty.
z[z>4]= np.nan
z[z<-4]= np.nan
Rather than using ax.plot_surface I found ax.plot_trisurf to work well, since you don't need to give it a rectangular grid of values like ax.plot_surface. If you're using numpy arrays, you can then use the following trick to only select points within your z-bounds.
from matplotlib import cm
x, y, z = x.flatten(), y.flatten(), z.flatten()
usable_points = (-4 < z) & (z < 4)
x, y, z = x[usable_points], y[usable_points], z[usable_points]
ax.plot_trisurf(x, y, z, cmap=cm.jet)

Categories

Resources