gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:38:19 26 4
gpt4 key购买 nike

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

输出是:

enter image description here

最佳答案

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

关于python - Matplotlib:将原点移动到左上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com