Jacobo22
Jacobo22

Reputation: 1

How i can plot this contour?

I would like to get this result without the circles:

enter image description here

This is my code:

from matplotlib import pyplot as plt                                                     
import numpy as np                                                                            
x = [-69,-60,-45,-30,-15,0.5,15,30,45,60,69,-69,-60,-45,-30,-15,0.5,15,30,45,60,69,-69,-60,-45,-30,-15,0.5,
 15,30,45,60, 69,-69,-60,-45,-30,-15,0.5,15,30,45,60,69,-69,-60,-45,-30,-15,0.5,15,30,45,60,69,-69,-60,
 -45,-30,-15,0.5,15,30,45,60,69,-69,-60,-45,-30,-15,0.5,15,30,45,60,69]                       
y = [31.5,31.5,31.5,31.5,31.5,31.5,31.5,31.5,31.5,31.5,31.5,30,30,30,30,30,30,30,30,30,30,30,24,24,24,24,24,24,24,
 24,24,24,24,18,18,18,18,18,18,18,18,18,18,18,12,12,12,12,12,12,12,12,12,12,12,6,6,6,6,6,6,6,6,6,6,6,0.5,0.5,0.5
 ,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5]                                                           
Up = [0.0762986, 0.151387, 0.209126, 0.234554, 0.244589, -0.245425, 0.238814, -0.222226, 0.20225736, 0.13785233,
  0.054526295, 0.0989059, 0.201592, 0.281129, 0.315983, 0.330039, -0.33129, 0.33185747, 0.286557, -0.254695
  ,-0.169509, 0.0621615, 0.157035, 0.334674, 0.479315, 0.545468, 0.573373, -0.577782, 0.57240381, -0.521733
  ,-0.440071, -0.284811, 0.0982288, 0.193492, 0.422493, 0.618154, 0.709479, 0.748067, -0.754549, 0.74816
  ,0.70471109, 0.59970581, 0.38760913, 0.13433145, 0.21642, 0.480047, 0.713037, 0.822546, 0.869416, -0.878781
  ,0.87194901, 0.82220797, 0.66209, 0.407971, 0.15190482, 0.229469, 0.512671, 7.68E-01, 0.888104, 0.940847
  ,-0.952318, 0.94503339, 0.89103107, 0.717762, 0.438857, 0.147151, 0.233621, 0.523136, 0.785145, 0.909226,
  0.964092, 0.976388, 0.96905899, 0.91365679, 0.77578289, 0.48991026, 0.150231]              
cs = plt.contourf(x, y, Up)                                                  
plt.colorbar(cs)

But I get this error:

TypeError: Input z must be 2D, not 1D

Upvotes: 0

Views: 81

Answers (1)

JohanC
JohanC

Reputation: 80339

If your data isn't on a regular rectangular grid, you can use plt.tricontourf(x, y, Up) instead of plt.contourf().

In this case, the data seems to lie on a 7x11 grid. You can reshape the 3 arrays to that 2D form:

from matplotlib import pyplot as plt
import numpy as np

x = [-69, -60, -45, -30, -15, 0.5, 15, 30, 45, 60, 69, -69, -60, -45, -30, -15, 0.5, 15, 30, 45, 60, 69, -69, -60, -45, -30, -15, 0.5, 15, 30, 45, 60, 69, -69, -60, -45, -30, -15, 0.5, 15, 30, 45, 60, 69, -69, -60, -45, -30, -15, 0.5, 15, 30, 45, 60, 69, -69, -60, -45, -30, -15, 0.5, 15, 30, 45, 60, 69, -69, -60, -45, -30, -15, 0.5, 15, 30, 45, 60, 69]
y = [31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
Up = [0.0762986, 0.151387, 0.209126, 0.234554, 0.244589, -0.245425, 0.238814, -0.222226, 0.20225736, 0.13785233, 0.054526295, 0.0989059, 0.201592, 0.281129, 0.315983, 0.330039, -0.33129, 0.33185747, 0.286557, -0.254695, -0.169509, 0.0621615, 0.157035, 0.334674, 0.479315, 0.545468, 0.573373, -0.577782, 0.57240381, -0.521733, -0.440071, -0.284811, 0.0982288, 0.193492, 0.422493, 0.618154, 0.709479, 0.748067, -0.754549, 0.74816, 0.70471109, 0.59970581, 0.38760913, 0.13433145, 0.21642, 0.480047, 0.713037, 0.822546, 0.869416, -0.878781, 0.87194901, 0.82220797, 0.66209, 0.407971, 0.15190482, 0.229469, 0.512671, 7.68E-01, 0.888104, 0.940847, -0.952318, 0.94503339, 0.89103107, 0.717762, 0.438857, 0.147151, 0.233621, 0.523136, 0.785145, 0.909226, 0.964092, 0.976388, 0.96905899, 0.91365679, 0.77578289, 0.48991026, 0.150231]

x = np.array(x).reshape(7, 11)
y = np.array(y).reshape(7, 11)
Up = np.array(Up).reshape(7, 11)
cs = plt.contourf(x, y, Up, cmap='turbo')
plt.colorbar(cs)

contourf of reshaped data

Upvotes: 1

Related Questions