Interpolating 3D heat map in matplotlib (XY,C)

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật