matplotlib默认的坐标原点在左下角;有时候你想将它变换到左上角。代码如下:
Code Example :
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
figure, ax = plt.subplots()
# 设置x,y值域
ax.set_xlim(left=0, right=20)
ax.set_ylim(bottom=10, top=0) # 此处将原点设置为左上角
ax.xaxis.tick_top() # 将x坐标标记移到上方
# 两条line的数据
line1 = [(1, 1), (5, 5)]
line2 = [(11, 9), (8, 8)]
(line1_xs, line1_ys) = zip(*line1)
(line2_xs, line2_ys) = zip(*line2)
# 创建两条线,并添加
ax.add_line(Line2D(line1_xs, line1_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line2_xs, line2_ys, linewidth=1, color='red'))
# 展示
plt.plot()
plt.show()
Output :
为了对照,你可以看这篇笔记 matplotlib画直线
Ref: