2

I've calculated some values representing a potential as a function of x,y using relaxation method. And I want to display a contour plot with colors (not lines) but, the examples at matplotlib are all fancy 3d plots. I have a ufinal object which is a 2 dimensional numpy array. I did see some nice answers with very nice plots here on SO but I wasn't able to use them properly with my data. I was able to plot a 3d plot using the examples but that's not what I need:

fig = plt.figure()
ax = fig.gca(projection='3d')

X,Y=meshgrid(x,y)

surf=ax.plot_surface(X,Y,ufinal,rstride=1,cstride=1,cmap=cm.jet,linewidth=0.1)
fig.colorbar(surf,shrink=0.5,aspect=5)

As suggested I've tried using the contourf example like so:

CS = plt.contourf(X, Y, ufinal,cmap=cm.jet)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')
SadStudent
  • 287
  • 1
  • 4
  • 16

2 Answers2

3

As David said, use contourf:

import numpy as np
import pylab as pl

x,y = np.mgrid[:1:1E-3,:1:1E-3]
xs = ((x-0.3)**2.)
ys = ((y-0.5)**2.)
z = np.exp(-1*(xs/0.5+ys/0.3))

pl.contourf(x,y,z,20)
ali_m
  • 71,714
  • 23
  • 223
  • 298
  • Thanks guys, I've tried using the basic example but it looks very ugly [2d contourf](http://postimg.org/image/np6k4z5m1/) at least the [3d image](http://postimg.org/image/mhbt86bg1/) looked nice and it at least created the illusion of being close to continuous. What am I doing wrong ? (pasted the code in the question) – SadStudent Jun 14 '13 at 12:29
  • That was just a very quick and dirty example for you :) I guess you could start out by increasing the number of contours you're drawing. If that doesn't look smooth enough for you, you can always evaluate your function in a dense grid then just `imshow` the result. – ali_m Jun 14 '13 at 12:36
  • Thanks you but could you elaborate on the last option? maybe there's an option to plot something like the tiles in the 3d plot but in 2d so it looks like a contour plot(because of the coloring) but it'll just try apply a color as a function of the value (e.g the lesser the number the more "bluish" it goes etc.) because as it seems increasing the number of lines just doesn't cut it, it's not granular enough. – SadStudent Jun 14 '13 at 12:48
  • For example, with the code I just gave you, try `imshow(z,cmap=pl.cm.jet)` and you'll get a 'smooth' representation of the 2D Gaussian, evaluated at intervals of 0.001 between 0 and 1 in the x and y dimensions. Note that (1) the x and y dimensions will appear swapped (because in this case different x-values map to the rows in `z`) and (2) the y-axis will appear inverted (since `imshow` places the origin in the upper left). You can also play around with different colormaps - try `cm.hot`, `cm.gray` etc. – ali_m Jun 14 '13 at 12:58
-1

In case anyone is still interested I found a solution for the granularity (a.k.a. nice-looking-ness problem) as part of the solution over here: Symmetrical Log color scale in matplotlib contourf plot

Community
  • 1
  • 1
Marcel
  • 51
  • 4
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes – Dethariel Aug 02 '16 at 17:45