2

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
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Jan 11, 2016 at 22:31
  • 2
    In what way is the plot "not really good"? Commented Jan 12, 2016 at 0:50

1 Answer 1

3

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:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@HYRY - On a side note, because the input data is already on a regular grid, scipy.interpolate.Rbf is not an efficient interpolation method. scipy.ndimage.zoom is more efficient for regularly-gridded input data, and would also be a few less lines of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.