0

I am trying to plot a filled contour using matplotlib. I am using imshow to plot the contour. How can I plot the contour at the size of the screen resolution.

For example, (x,y) are coordinates and z is corresponding value to the location:

import pylab as plt
import numpy as np
from Tkinter import *
root = Tk()
root.title("test window")
root.minsize(800, 600)
#(x,y) are pixel coordinates of canvas created on root, z= values
x=(563.603522282, 405.223798299,91.082456699,418.716359588,86.4813516941,244.703807775,480.84654939,418.702483547,241.136696875,319.613136686,257.545407017,117.714659717,174.788434125,425.913041522)
y=(96.3119943139,65.6348405695,353.988303883,354.795931878,170.857459385,156.952879623,161.233412703,241.960699493,214.026654031,65.6348405695,63.4945740292,54.2200856878,7.13422180103,3.56711090052)
z=(-1, 0, 0.5, -0.2, 1, 6, 0, -5, 4, 1.5, 2,7,-3,1)
Z=np.array((x,y,z)) #not sure from here
im = plt.imshow(Z, cmap='jet') 
root.crate_image(im) # the image created by imshow paste on canvas
mainloop()

above is the basic idea of my code. but i'm not getting how to do. please help

Ram Krix
  • 1
  • 1
  • @Veedrac added my basic code it's just idea not a working code – Ram Krix Sep 20 '14 at 14:03
  • OK, thanks. It wasn't obvious that you intended to do this through Tkinter! I'm afraid that's a bit too far outside of my comfort zone. – Veedrac Sep 20 '14 at 14:09
  • You need to explain what your data mean and what story you're trying to tell with them. As written, your data are nowhere near appropriate for `imshow` or `contourf`. – Paul H Sep 20 '14 at 14:34
  • @PaulH Thank you for responding. My coordinates are the points on my screen and based on the values i want to show filled contour plot. in other words, my contour plot dimensions should match with the screen resolution or dimensions. – Ram Krix Sep 20 '14 at 17:56

1 Answers1

1

imshow wants an NxM array that kind of lives out in space on its own.

You have three vectors, so this question you're really asking is:

How do you interpolate data with a random spatial distribution onto a regularly-spaced grid?

The answer to that is the following:

As written right now, your data would most easily be shown in a scatter plot:

import matplotlib.pyplot as plt # don't use pylab
import numpy as np

x= np.array([
    563.603522282, 405.223798299, 91.082456699,
    418.716359588, 86.4813516941, 244.703807775,
    480.84654939,  418.702483547, 241.136696875,
    319.613136686, 257.545407017, 117.714659717,
    174.788434125, 425.913041522
])
y = np.array([
    96.3119943139, 65.6348405695, 353.988303883,
    354.795931878, 170.857459385, 156.952879623,
    161.233412703, 241.960699493, 214.026654031,
    65.6348405695, 63.4945740292, 54.2200856878,
    7.13422180103, 3.56711090052
])

z = np.array([-1, 0, 0.5, -0.2, 1, 6, 0, -5, 4, 1.5, 2,7,-3,1])

# define grid on which you'll interpolate.
N = 100
xi = np.linspace(np.floor(x.min()), np.ceil(x.max()), N)
yi = np.linspace(np.floor(y.min()), np.ceil(y.max()), N)

# grid (interpolate) the data.
zi = plt.mlab.griddata(x, y, z, xi, yi, interp='linear')

# contour the gridded data, plotting dots at the nonuniform data points.
fig, ax = plt.subplots()
c = ax.contour(xi, yi, zi, 15, linewidths=0.5, colors='k')
cf = ax.contourf(xi, yi, zi, 15, cmap=plt.cm.coolwarm,  # seriously, don't use jet or rainbow or anything like that
                  vmax=abs(zi).max(), vmin=-abs(zi).max())
fig.colorbar(cf)  # draw colorbar
# plot data points.
ax.scatter(x, y, marker='o', c='b', s=5, zorder=10)

enter image description here

At this point, you can use imshow or pcolor to show zi as an image:

fig, ax = plt.subplots()
pc = ax.pcolor(xi, yi, zi, cmap=plt.cm.coolwarm)
fig.colorbar(pc)
fig.savefig('pcolor.png')

enter image description here

I use pcolor because setting the extents of the x- and y-axes is more intuitive.

Paul H
  • 65,268
  • 20
  • 159
  • 136
  • 1
    Thank you for responding. My coordinates are the points on my screen and based on the values i want to show filled contour plot. in other words, my contour plot dimensions should match with the screen resolution or dimensions. – Ram Krix Sep 20 '14 at 17:55
  • 1
    @RamKrix -- you keep saying that, but not providing any examples showing what you mean. So I've taken another guess. How does the edited response look? – Paul H Sep 20 '14 at 22:42