Contour Plot of 2D Array in Matplotlib

Contour Plot of 2D Array in Matplotlib

I have the following code and am trying to plot a contour plot of my data. I tried following some few example from the forum with no success. The plot I produce running this code is not really good. Any advice will be greatly appreciated. Thank you in advance for your assistance.

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()
2

1 Answer

I think your is not really good means contour is not smooth, Here are two methods to interpolate the data:

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]);

the output:

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.