Skip to main content
 首页 » 编程设计

python之Matplotlib:将原点移动到左上角

2025年05月04日13cloudgamer

在 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. 查看([-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]) 

输出是:

请您参考如下方法:

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

图片: