Matplotlib插值空像素

Matplotlib插值空像素,matplotlib,scipy,interpolation,Matplotlib,Scipy,Interpolation,我有一个文件“mydata.tmp”,其中包含3个列,如下所示: 3.81107 0.624698 0.000331622 3.86505 0.624698 0.000131237 3.91903 0.624698 5.15136e-05 3.97301 0.624698 1.93627e-05 1.32802 0.874721 1.59245 1.382 0.874721 1.542 1.43598 0.874721 1.572 1.48996 0.874721 4.27933

我有一个文件“mydata.tmp”,其中包含3个列,如下所示:

3.81107 0.624698 0.000331622 
3.86505 0.624698 0.000131237 
3.91903 0.624698 5.15136e-05 
3.97301 0.624698 1.93627e-05 
1.32802 0.874721 1.59245 
1.382   0.874721 1.542
1.43598 0.874721 1.572 
1.48996 0.874721 4.27933 
等等

然后我想做一个热图颜色图,其中前两列是坐标,第三列是坐标的值

另外,我想在log scale中设置第三列

我已经这样做了

import pandas as pd
import matplotlib.pyplot as plt
import scipy.interpolate
import numpy as np
import matplotlib.colors as colors

# import data
df = pd.read_csv('mydata.tmp', delim_whitespace=True, 
                 comment='#',header=None,
                 names=['1','2','3'])

x = df['1']
y = df['2']
z = df['3']

spacing = 500
xi, yi = np.linspace(x.min(), x.max(), spacing), np.linspace(y.min(), 
                 y.max(), spacing)

XI, YI = np.meshgrid(xi, yi)

rbf = scipy.interpolate.Rbf(x, y, z, function='linear')

ZI = rbf(XI, YI)

fig, ax = plt.subplots()

sc = ax.imshow(ZI, vmin=z.min(), vmax=z.max(), origin='lower',
        extent=[x.min(), x.max(), y.min(), 
                y.max()], cmap="GnBu", norm=colors.LogNorm(vmin=ZI.min(),
                vmax=ZI.max()))

fig.colorbar(sc, ax=ax, fraction=0.05, pad=0.01)

plt.show()
我得到了这个图像

它有所有这些空像素

我正在寻找类似的东西(我用GNUplot完成了另一张图片):

如何操作?

您可以使用定义NaN值的颜色:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata

import matplotlib.colors as colors
from matplotlib import cm
import copy

# Some data
x = np.array([0, 1, 3, 0, 2, 4])
y = np.array([0, 0, 0, 1, 1, 1])
z = np.array([2, 2, 3, 2, 3, 4])

# Interpolation on a grid:
nrb_points = 101
xi = np.linspace(-.5, 4.5, nrb_points)
yi = np.linspace(-.5, 1.5, nrb_points)

XI, YI = np.meshgrid(xi, yi)
xy = np.vstack((x, y)).T
XY = (XI.ravel(), YI.ravel())

ZI = griddata(points, z, XY,
              method='linear',
              fill_value=np.nan) # Value used [for] points
                                 # outside of the convex hull
                                 # of the input points.
ZI = ZI.reshape(XI.shape)

# Color map:
cmap = copy.copy(cm.jet)
cmap.set_bad('grey', 1.)

# Graph:
plt.pcolormesh(xi, yi, ZI,
               #norm=colors.LogNorm(),
               cmap=cmap);
plt.colorbar(label='z');
plt.plot(x, y, 'ko');
plt.xlabel('x'); plt.ylabel('y');
结果是:


我还将使用RBF方法代替插值。然后,输入数据区域外的点(即凸包)可以设置为NaN。

看起来其中一个图是错误的。然而,我们无法找出它是哪一个,因为我们没有数据。r=3000,log(E)=3附近区域的数据是多少?是0还是10^-7?