1. 程式人生 > >matplotlib 座標原點移到左上角

matplotlib 座標原點移到左上角

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 :
旋轉圖

Ref: