0

I am using contour or contourf in matplotlib

And the data is a 2D array with values in, like this:

1 2 3 3 3
2 3 3 4 1
2 3 4 5 6
...

The result I got is as below. enter image description here

It is like a square, while actually, the y extent is 600+ and x extent is only 350. So the figure should look like a rectangle, not a square.

But I looked into the arguments in contour and contourf, there is no argument about changing the shape of the contour, or changing the length of the axis.

for Adobe, here is the simplified code of my case:

import matplotlib.pyplot as plt

m = [[1,2,3,4],
[2,3,4,5],
[2,2,1,5]]

print m
plt.contourf(m)
plt.show()

Then, in this situation, how to use ax.axis()?

rankthefirst
  • 1,370
  • 2
  • 14
  • 26

1 Answers1

3

Probably You want to set equal scale:

ax.axis('equal')

Edit

Here's Your code:

#!/usr/bin/python3

from matplotlib import pyplot as plt

m = [[1,2,3,4],
     [2,3,4,5],
     [2,2,1,5]]

fig = plt.figure()
ax = fig.add_subplot(111)

ax.contourf(m)
ax.axis('equal')

fig.savefig("equal.png")

enter image description here

matplotlib has three interfaces. Here's the same code written to utilize each of them:

  1. machine-state:

    import matplotlib.pyplot as plt
    import numpy as np
    x = np.arange(0, 10, 0.2)
    y = np.sin(x)
    plt.plot(x, y)
    plt.show()
    
  2. pylab:

    from pylab import *
    x = arange(0, 10, 0.2)
    y = sin(x)
    plot(x, y)
    show()
    
  3. object-oriented:

    import matplotlib.pyplot as plt
    import numpy as np
    x = np.arange(0, 10, 0.2)
    y = np.sin(x)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(x, y)
    plt.show()
    

I prefer object-oriented interface: it gives full control on what is going on. I cited solution for that one.

Adobe
  • 12,967
  • 10
  • 85
  • 126
  • Thank you for that, but do I have to import which lib? It doesn't work maybe because I need do something else, but this did help me to find the solution, thanks. – rankthefirst Aug 23 '14 at 21:43
  • @rankthefirst: I can't tell -- unless You post minimal working example reproducing Your problem. You shouldn't import anything for this solution. But Yo might have used pylab interface, while I posted a solution tested with pyplot interface. See [here](http://matplotlib.org/faq/usage_faq.html#matplotlib-pylab-and-pyplot-how-are-they-related). – Adobe Aug 24 '14 at 06:34