matplotlib

时间:2016-01-11 22:27:53

标签: arrays numpy matplotlib 2d

我有以下代码,并试图绘制我的数据的等高线图。我尝试从论坛中关注一些例子但没有成功。我运行此代码的情节并不是很好。任何建议将不胜感激。提前感谢您的协助。

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

N = 1000  # number of points for plotting/interpolation

FR = np.array([[0.763, 0.762, 0.954, 0.000, 0.835, 0.000],
               [0.000, 1.052, 1.080, 1.176, 0.864, 0.811],
               [1.179, 1.148, 1.368, 0.000, 1.147, 0.000],
               [0.000, 1.279, 1.315, 1.434, 1.031, 0.880],
               [1.176, 1.134, 1.355, 0.000, 1.131, 0.000],
               [0.000, 1.008, 1.045, 1.092, 0.840, 0.724],
               [0.672, 0.682, 0.755, 0.708, 0.643, 0.000]])

x = np.arange(1, 7)
y = np.arange(7, 1 + (-1), -1)

# Set up a regular grid of interpolation points
# xi, yi = np.linspace(x.min(), x.max(), N), np.linspace(y.min(), y.max(), N)
# xi, yi = np.meshgrid(xi, yi)

# Interpolate
# rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
# zi = rbf(xi, yi)

# plt.imshow(zi, vmin=z.min(), vmax=z.max(), origin='lower',
#           extent=[x.min(), x.max(), y.min(), y.max()])
# plt.scatter(x, y, c=z)
# plt.colorbar()
# plt.show()
# plt.imshow(FR, interpolation='nearest')

# xi = np.linspace(x.min(), x.max(), N)
# yi = np.linspace(y.min(), y.max(), N)
# zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')

# fig = plt.figure()
# plt.contour(xi, yi, zi)
# plt.xlabel("X")
# plt.ylabel("Y")
# plt.show()


# plt.pcolor()
# plt.colorbar()
plt.contourf(FR)
plt.axis('off')
plt.grid()
plt.show()

1 个答案:

答案 0 :(得分:3)

我认为不是很好意味着轮廓不平滑,以下是两种插值数据的方法:

import pylab as pl
from matplotlib import cm
import numpy as np
from scipy import interpolate
from scipy import ndimage

FR = np.array([[0.763, 0.762, 0.954, 0.000, 0.835, 0.000],
               [0.000, 1.052, 1.080, 1.176, 0.864, 0.811],
               [1.179, 1.148, 1.368, 0.000, 1.147, 0.000],
               [0.000, 1.279, 1.315, 1.434, 1.031, 0.880],
               [1.176, 1.134, 1.355, 0.000, 1.131, 0.000],
               [0.000, 1.008, 1.045, 1.092, 0.840, 0.724],
               [0.672, 0.682, 0.755, 0.708, 0.643, 0.000]])

X, Y = np.mgrid[0:1:7j, 0:1:6j]

fig, axes = pl.subplots(1, 3, figsize=(15, 4))
c1 = axes[0].contourf(X, Y, FR)
pl.colorbar(c1, ax=axes[0]);

tmp = np.repeat(np.repeat(FR, 10, axis=1), 10, axis=0)
x, y = np.mgrid[0:1:70j, 0:1:60j]
c2 = axes[1].contourf(x, y, ndimage.gaussian_filter(tmp, 3), levels=c1.levels)
pl.colorbar(c2, ax=axes[1]);

rbf = interpolate.Rbf(X.ravel(), Y.ravel(), FR.ravel(), smooth=0.000001)

X2, Y2 = np.mgrid[0:1:70j, 0:1:60j]

c3 = pl.contourf(X2, Y2, rbf(X2, Y2))
pl.colorbar(c3, ax=axes[2]);

输出:

enter image description here