rankthefirst
rankthefirst

Reputation: 1468

how to change the shape of a contour in matplotlib

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()?

Upvotes: 0

Views: 1996

Answers (1)

Adobe
Adobe

Reputation: 13477

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.

Upvotes: 3

Related Questions