'How to draw a continuous contour plot with discrete coordinate data (DataFrame form)?

The row data has 3 columns and cannot shape a uniform grid based on 'x'&'z', so I am not able to plot the contour as the existed question: Create Contour Plot from Pandas Groupby Dataframe.

The row data is attached here (updated): https://drive.google.com/drive/folders/1nm84hJynYK0d6J-ToRT9oiZZFt942RnJ?usp=sharing

I attempted to divide the data into 2 groups by z values but get a plot with blank areas:

df1 = pd.read_pickle('sample.pkl')
zone1 = df1[(df1['z'].between(0,4.8))]
zone2 = df1[(df1['z'].between(4.8,30))]

piv1 = zone1.pivot('x','z')
piv2 = zone2.pivot('x','z')

fig = plt.figure(figsize=(20,10),dpi=300)
vmin= 5.2e-6
vmax= 9e-6
levels=np.linspace(vmin,vmax,50)
    
ax1= fig.add_subplot(1,1,1) 
X1=piv1.columns.levels[1].values
Y1=piv1.index.values
Z1=piv1.values
Xi1,Yi1 = np.meshgrid(X1, Y1)
X2=piv2.columns.levels[1].values
Y2=piv2.index.values
Z2=piv2.values
Xi2,Yi2 = np.meshgrid(X2, Y2)
cs1 = ax1.contourf(Yi1, Xi1, Z1, levels=levels,vmax=vmax,vmin=vmin,alpha=0.9, cmap=plt.cm.jet)
cs2 = ax1.contourf(Yi2, Xi2, Z2, levels=levels,vmax=vmax,vmin=vmin,alpha=0.9, cmap=plt.cm.jet)

plot1

I have also attempted 2D interpolation but cannot use the scipy.interpolate.interp2d right.

How can I get the continuous contourf without blank areas when part of the data is lost?

Update:

When I don't divide them and use pivot for plotting, it shows as below: fig2

The row data has the characteristics: fig3



Solution 1:[1]

Ok, I think I understand the problem now and I agree you can't just plot the whole thing at once.

I think the quickest fix that would look like you want would be to do some quick interpolation and then make the plot.

piv0 = df1.pivot('x','z')
X0=piv0.columns.levels[1].values
Y0=piv0.index.values
Z0=piv0.values
Z0int = piv0.interpolate(method='linear',limit_direction='both')
cs0 = ax1.contourf(Yi0, Xi0, Z0int, levels=levels,vmax=vmax,vmin=vmin,alpha=0.9, cmap=plt.cm.jet)

enter image description here

This interpolation is pretty crude (1D), but I think it looks as you wanted. If you want something a little better I would go to scipy (as you suggested) and do the interpolation you want and then do the plotting. I think griddata would be better than interp2d though. Some examples here: https://scipython.com/book/chapter-8-scipy/examples/two-dimensional-interpolation-with-scipyinterpolategriddata/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pasnik