Reputation: 330
Hi iam trying to plot a filled contour of uneven data. which is in a three lists. My problem here is i can't able to get smooth filled contour. what i did is first i changed my data from a irregular points to a grid using griddata.
import numpy as np
import matplotlib.pyplot as plt
import time as time
from scipy.interpolate import griddata
x = [39, 39, 603, 603, 540.8578720591851, 586.349172503832, 373.99215228030187, 436.4554443169055, 125.7177128362948, 56.44720056160912, 453.35159098310174, 384.081128192362, 51.846094630755104, 121.11660875746472, 278.0734642496455, 211.33415130113278, 508.642428513517, 453.0506702655636, 455.66065332357397, 381.7443137710119, 211.08060937414135, 271.19278437560484, 301.7212739516758, 337.50499942076925, 237.27644459337762, 277.8143694411149, 89.76821876085899, 145.66110067318877, 151.97990283138796, 197.59696541916784, 398.0895764975718, 453.7365065456195]
y = [-29, 394, -29, 394, 96.31199431392861, 96.31199431392861, 65.63484056949213, 65.63484056949213, 353.9802948050525, 353.99631296027843, 354.83809861715105, 354.75376513965614, 170.85745938538898, 170.85745938538898, 156.95287962269862, 156.95287962269862, 161.4804871844196, 160.98633822221555, 242.17985596076556, 241.74154302501933, 214.02665403095247, 214.02665403095247, 65.63484056949213, 65.63484056949213, 63.49457402918261, 63.49457402918261, 54.22008568784131, 54.22008568784131, 7.134221801031751, 7.134221801031751, 3.5671109005158756, 3.5671109005158756]
z = [0, 0, 0, 0, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
extent = (min(x), max(x), min(y), max(y))
xs,ys = np.mgrid[extent[0]:extent[1], extent[2]:extent[3]]
resampled = griddata((x, y), z, (xs, ys))
plt.figure()
plt.imshow(resampled.T, extent=(min(x), max(x), max(y), min(y)))
plt.hold(True)
plt.scatter(x,y,c=z)
plt.show()
which is giving plot as below.
Which is looking odd. How can i smooth the color variation to the next point.
Thanks in advance
Upvotes: 2
Views: 8492
Reputation: 2823
Your sampling mesh is too dense so that imshow does not have to interpolate at all. With a size of around 30x30 points it looks much better. Also, set the interpolation method of imshow to 'bicubic'.
Edit: I overlooked that griddata's interpolation method can be set to 'cubic'. This yields even better results and you may leave the sampling size as before.
Edit2: It looks like the best results are achieved with my original approach. Let griddata generate samples on a grid by linear interpolation and use imshow or contour to do cubic interpolation based on that data, i.e:
import numpy as np
import matplotlib.pyplot as plt
import time as time
from scipy.interpolate import griddata
x = [39, 39, 603, 603, 540.8578720591851, 586.349172503832, 373.99215228030187,436.4554443169055, 125.7177128362948, 56.44720056160912, 453.35159098310174, 384.081128192362, 51.846094630755104, 121.11660875746472, 278.0734642496455, 211.33415130113278, 508.642428513517, 453.0506702655636, 455.66065332357397, 381.7443137710119, 211.08060937414135, 271.19278437560484, 301.7212739516758, 337.50499942076925, 237.27644459337762, 277.8143694411149, 89.76821876085899, 145.66110067318877, 151.97990283138796, 197.59696541916784, 398.0895764975718, 453.7365065456195]
y = [-29, 394, -29, 394, 96.31199431392861, 96.31199431392861, 65.63484056949213, 65.63484056949213, 353.9802948050525, 353.99631296027843, 354.83809861715105, 354.75376513965614, 170.85745938538898, 170.85745938538898, 156.95287962269862, 156.95287962269862, 161.4804871844196, 160.98633822221555, 242.17985596076556, 241.74154302501933, 214.02665403095247, 214.02665403095247, 65.63484056949213, 65.63484056949213, 63.49457402918261, 63.49457402918261, 54.22008568784131, 54.22008568784131, 7.134221801031751, 7.134221801031751, 3.5671109005158756, 3.5671109005158756]
z = [0, 0, 0, 0, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
extent = (min(x), max(x), min(y), max(y))
xs,ys = np.mgrid[extent[0]:extent[1]:30j, extent[2]:extent[3]:30j]
resampled = griddata((x, y), z, (xs, ys))
plt.figure(figsize=(8,8))
plt.imshow(resampled.T, extent=(min(x), max(x), max(y), min(y)),interpolation='bicubic')
plt.contour(resampled.T, extent=(min(x), max(x), max(y), min(y)),interpolation='bicubic',origin='upper')
plt.hold(True)
plt.scatter(x,y,c=z)
plt.show()
Upvotes: 2