Matplotlib:将Origin移动到左上角

时间:2015-03-12 14:37:46

标签: python matplotlib matlab-figure

在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])
  4. 修改

    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])
    

    输出是:

    enter image description here

1 个答案:

答案 0 :(得分:6)

这是一个注释示例,显示如何反转轴并将刻度移动到顶部,设置网格状态并模仿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

图像: enter image description here