首页 文章

如何在matplotlib中更改轮廓的形状

提问于
浏览
0

我在 matplotlib 使用 contourcontourf

数据是一个带有值的二维数组,如下所示:

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

我得到的结果如下 .
enter image description here

它就像一个正方形,而实际上,y范围是600,x范围只有350.所以图形应该看起来像一个矩形,而不是一个正方形 .

但是我查看了 contourcontourf 中的参数,没有关于改变轮廓形状或改变轴长度的论点 .

对于Adobe,这是我案例的简化代码:

将matplotlib.pyplot导入为plt

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

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

那么,在这种情况下,如何使用ax.axis()?

1 回答

  • 2

    可能你想设置相同的比例:

    ax.axis('equal')
    

    Edit

    这是你的代码:

    #!/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有三个接口 . 这是为了利用它们而编写的相同代码:

    • 机器状态:
    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()
    
    • pylab:
    from pylab import *
    x = arange(0, 10, 0.2)
    y = sin(x)
    plot(x, y)
    show()
    
    • 面向对象:
    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()
    

    我更喜欢面向对象的界面:它可以完全控制正在发生的事情 . 我引用了那个解决方案 .

相关问题