I have plotted X,Y co-ordinates in a 2D polar-plot, and I am passing a third value to each co-ordinate that is my third dimension. I am trying to the interpolate between all plotted points, similar to the example found here: Circular interpolated heat map plot using python.
I am however not getting the result I am expecting.
Below is my shortened code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
### INTENSITY VALUES FOR HEATMAP ###
T1 = 100,100,100,100,100,100,50,50,50,50,50,50,1,1,1,1,1,1,
### CO-ORDINATES ###
a=0
b=0
rO = 7.5
rM = 5
rI = 2.5
heights = 1,2,3,4,5
### RADIAL POINTS ###
def XCoords(a,rO,rM,rI):
XsX = []
O = np.linspace(0,2*np.pi,6,endpoint=False)
for i in O:
xx = a+rO*np.cos(i),a+rM*np.cos(i),a+rI*np.cos(i)
XsX.append(xx)
return XsX
Xs1,Xs2,Xs3 = np.column_stack(XCoords(a,rO,rM,rI))
def YCoords(b,rO,rM,rI):
YsY = []
O = np.linspace(0,2*np.pi,6,endpoint=False)
for i in O:
yy = a+rO*np.sin(i),a+rM*np.sin(i),a+rI*np.sin(i)
YsY.append(yy)
return YsY
Ys1,Ys2,Ys3 = np.column_stack(YCoords(b,rO,rM,rI))
def cart2pol(x,y):
rho = np.sqrt(x**2 + y**2)
phi = np.arctan2(y,x)
return (rho,phi)
PolarR1, PolarT1 = cart2pol(Xs1,Ys1)
PolarR2, PolarT2 = cart2pol(Xs2,Ys2)
PolarR3, PolarT3 = cart2pol(Xs3,Ys3)
PolarRF = PolarR1,PolarR2,PolarR3
PolarTF = PolarT1,PolarT2,PolarT3
### INTERPOLATION ATTEMPT ###
max_r = 7.5
max_theta = 2*np.pi
theta = np.linspace(0,max_theta,200)
r = np.linspace(0,max_r,200)
grid_r,grid_theta = np.meshgrid(r,theta)
X,Y = np.meshgrid(PolarRF,PolarTF)
points = np.column_stack((X.flatten(),Y.flatten()))
values = np.linspace(np.min(T1),np.max(T1),324)
data = griddata(points,values,(grid_r,grid_theta),method='nearest',fill_value=0,rescale=False)
### HEAT MAP COLORS ###
grad = 'viridis'
cA = T1
mB = plt.cm.ScalarMappable(cmap=grad)
fAcolors = mB.to_rgba(cA)
### PLOT ###
fig = plt.figure(figsize=(15,15), dpi=300)
ax = fig.add_subplot(121,projection='polar')
ax.scatter(PolarTF,PolarRF,s=200,c=fAcolors,alpha=0.5)
fig2 = plt.figure(figsize=(15,15),dpi=300)
ax2 = fig.add_subplot(122,projection='polar')
ax2.pcolormesh(grid_theta,grid_r,data,alpha=0.5)
plt.show()
This code produces the plot:
I am unable to get the second plot to have a distribution that is similar to the first. I’m unsure what I am doing wrong. I have tried changing which method I use, and this doesn’t produce the results I want either.
4
It doesn’t seem like you should be invoking scipy’s interpolation; instead, you just want continuous (Gouraud) shading on pcolormesh
.
import numpy as np
import matplotlib.pyplot as plt
polar_rf = np.array((2.5, 5.0, 7.5))
t1_intensities = np.array((1, 50, 100))
theta = np.linspace(start=0, stop=2*np.pi, num=51)
X, Y = np.meshgrid(theta, polar_rf)
C = np.broadcast_to(t1_intensities[:, np.newaxis], shape=X.shape)
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.pcolormesh(X, Y, C, shading='gouraud')
plt.show()
13
Update: I have found a way to make this work. My biggest issue was not having the three variables gridtdf
, gridrdf
, and data3.T
in the correct forms to match up correctly, which was causing me issues.
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
import pandas as pd
### INTENSITY VALUES FOR HEATMAP ###
Ti = 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Tmi = 25,25,25,25,25,25,25,25,25,25,25,25,25,25,25
Tm = 50,50,50,50,50,50,50,50,50,50,50,50,50,50,50
To = 100,100,100,100,100,100,100,100,100,100,100,100,100,100,100
Tall = Ti,Tmi,Tm,To
Tarr = np.array(Tall)
Tcs = np.column_stack(Tarr)
Tdf = pd.DataFrame(Tall)
Tzall = To+Tm+Tmi+Ti
T1r = []
for i in Tdf:
xx = Tdf.iloc[:,i]
T1r.append(xx)
T1rr = pd.concat(T1r)
### CO-ORDINATES ###
a=0
b=0
rO = 7.5
rM = 5.5
rMi = 3.5
rI = 1.5
heights = 1,2,3,4,5
### RADIAL POINTS ###
def XCoords(a,rO,rM,rMi,rI):
XsX = []
O = np.linspace(0,2*np.pi,15,endpoint=True)
for i in O:
xx = (a+rI*np.cos(i)),(a+rMi*np.cos(i)),(a+rM*np.cos(i)),(a+rO*np.cos(i))
XsX.append(xx)
return XsX
Xs1,Xs2,Xs3,Xs4 = np.column_stack(XCoords(a,rO,rM,rMi,rI))
Xss = np.array(XCoords(a,rO,rM,rMi,rI))
Xssdf = pd.DataFrame(Xss)
Xfi = []
for i in Xssdf:
xx = Xssdf.iloc[:,i]
Xfi.append(xx)
Xfii = pd.concat(Xfi)
def YCoords(b,rO,rM,rMi,rI):
YsY = []
O = np.linspace(0,2*np.pi,15,endpoint=True)
for i in O:
yy = a+rI*np.sin(i),a+rMi*np.sin(i),a+rM*np.sin(i),a+rO*np.sin(i)
YsY.append(yy)
return YsY
Ys1,Ys2,Ys3,Ys4 = np.column_stack(YCoords(b,rO,rM,rMi,rI))
Yss = np.array(YCoords(b,rO,rM,rMi,rI))
Yssdf = pd.DataFrame(Yss)
Yfi = []
for i in Yssdf:
xx = Yssdf.iloc[:,i]
Yfi.append(xx)
Yfii = pd.concat(Yfi)
def cart2pol(x,y):
rho = np.sqrt(x**2 + y**2)
phi = np.arctan2(y,x)
return (rho,phi)
PolarR1, PolarT1 = cart2pol(Xs1,Ys1)
PolarR2, PolarT2 = cart2pol(Xs2,Ys2)
PolarR3, PolarT3 = cart2pol(Xs3,Ys3)
PolarR4, PolarT4 = cart2pol(Xs4,Ys4)
PolarRF = PolarR4,PolarR3,PolarR2,PolarR1
PolarTF = PolarT4,PolarT3,PolarT2,PolarT1
### INTERPOLATION ATTEMPT ###
max_r = 7.5
max_theta = 2*np.pi
theta = np.linspace(0,max_theta,500)
r = np.linspace(0,max_r,500)
grid_r,grid_theta = np.meshgrid(r,theta)
gridrdf = pd.DataFrame(grid_r)
gridtdf = pd.DataFrame(grid_theta)
def pol2cart(rho, phi):
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return(x, y)
gridx, gridy = pol2cart(grid_r,grid_theta)
points = np.column_stack(((Xfii,Yfii)))
pointsdf = pd.DataFrame(points)
vals = []
for i in Tdf:
length = int(len(pointsdf)/15)
test = np.linspace(np.min(Tdf.iloc[:,i]),np.max(Tdf.iloc[:,i]),length)
vals.append(test)
valsdf = pd.DataFrame(vals)
valscon = []
for i in valsdf:
xx = (valsdf.iloc[:,i])
valscon.append(xx)
valsf = pd.concat(valscon)
data = griddata(pointsdf,valsf,(gridx,gridy),method='linear',fill_value=100,rescale=False)
data2 = np.column_stack(data)
data3 = pd.DataFrame(data2)
### HEAT MAP COLORS ###
grad = 'viridis'
cA = Tzall
mB = plt.cm.ScalarMappable(cmap=grad)
fAcolors = mB.to_rgba(cA)
### PLOT ###
fig = plt.figure(figsize=(15,15), dpi=300)
ax = fig.add_subplot(121,projection='polar')
ax.scatter(PolarTF,PolarRF,s=200,c=fAcolors,alpha=1)
fig2 = plt.figure(figsize=(15,15),dpi=300)
ax2 = fig.add_subplot(122,projection='polar')
ax2.pcolormesh(gridtdf,gridrdf,data3.T,alpha=1)
plt.show()
10