Search code examples
pythonnumpymatplotlibheatmappolar-coordinates

Plot a 1D array on 3 radii in a polar heat map


I have a 3 1D arrays: radius, angle and temperature. Together they form a 2D temperature map of a ring. The arrays take the form:

r = [0,0,0,1,1,1,2,2,2]
th = [0.,0.78539816,1.57079633,2.35619449,3.14159265,3.92699082,4.71238898,5.49778714,6.28318531]
z = [-1,2,5,2,4,-1,3,2,3]

I don't understand how I can make those z data fall on the right coordinates.

I can make it work, with random numbers, using the following simple code:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = Axes3D(fig)

rad = np.linspace(.2, 1, 4)
azm = np.linspace(0, 2 * np.pi, 9)
r, th = np.meshgrid(rad, azm)
z = np.random.rand(9,4) ** th * r
ax0 = plt.subplot(projection="polar")
im = plt.pcolormesh(th, r, z, cmap='bwr')
plt.plot(azm, r, color='k', ls='none')
plt.axis('off')
cbar = fig.colorbar(im)
ax0.set_title('3 radii polar heat map')

This is how my example code comes out


Solution

  • I ended splitting the list z into 3 lists and made a matrix out of them using z_mat = np.array([z1,z2,z3]). I took care that the lists for rad and azm contained one item more than z, which is a requirement for pcolormesh. After that I transposed the matrix to have the same dimensions as r and th using z_mat_trans = z_mat.transpose()