【问题标题】:Matplotlib: move Origin to upper left cornerMatplotlib:将原点移动到左上角
【发布时间】:2015-03-12 14:37:46
【问题描述】:

在Matlab中,绘图是这样制作的,原点在左上角,x轴在原点的南边为正,y轴在原点的东边为正; x 轴在左边距编号,y 轴在上边距标记。

figure();
set(gca,'YAxisLocation','Right','YDir','reverse')
axis([0 10 0 10]);
daspect([1,1,1])
grid on
view([-90 -90])

如何在 Python 中实现相同的功能?我继续这样:

import matplotlib.pyplot as plt
plt.figure(1)

plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()

python 的等价物是什么:

  1. set(gca,'YAxisLocation','Right','YDir','reverse')
  2. daspect([1,1,1])
  3. view([-90 -90])

编辑:

figure
set(gca,'YAxisLocation','right','XTick',0:15,'YDir','reverse')
axis([0 16 0 16]);
daspect([1,1,1])
hold on
text(2,8,'CHN');
text(8,2,'USA');
view([-90 -90])

输出是:

【问题讨论】:

    标签: python matplotlib matlab-figure


    【解决方案1】:

    这是一个注释示例,显示了如何反转轴并将刻度移动到顶部,设置网格状态并模仿 view() 命令:

    import matplotlib.pyplot as plt
    import numpy as np
    
    plt.figure()    
    plt.axis([0, 16, 0, 16])     
    plt.grid(False)                         # set the grid
    data= [(8,2,'USA'), (2,8,'CHN')]
    
    for obj in data:
        plt.text(obj[1],obj[0],obj[2])      # change x,y as there is no view() in mpl
    
    
    ax=plt.gca()                            # get the axis
    ax.set_ylim(ax.get_ylim()[::-1])        # invert the axis
    ax.xaxis.tick_top()                     # and move the X-Axis      
    ax.yaxis.set_ticks(np.arange(0, 16, 1)) # set y-ticks
    ax.yaxis.tick_left()                    # remove right y-Ticks
    

    图片:

    【讨论】:

    • 我使用的是matplotlib,而不是pylab。你能用matplotlib 给出你的答案还是一样?
    • 是的,太好了。我还想以一种特殊的方式查看图形(我想从代码生成的 x-y 平面下方查看图形,头朝西,脚朝东),正如我在问题中所说。在 Matlab 中,我知道 view([-90 -90]) 可以解决问题。用 Python 做什么?
    • 您能给我看一个包含视图转换前后数据的示例图吗?我无法安静地知道它应该是什么样子。一般来说,您可以使用 plt.axis( [xmin, xmax, ymin, ymax] ) 命令来更改视口。例如:只需交换 xmin 和 xmax 即可绕 y 轴翻转。
    • 我更新了我的答案。不幸的是,没有像 view() 命令 afaik 这样的东西。不过,您可以通过切换数据来模仿。
    • 你可以用ax.invert_yaxis()反转y轴。
    猜你喜欢
    • 2010-11-23
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多