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 :
旋转图

为了对照,你可以看这篇笔记 matplotlib画直线


Ref:

Matplotlib: move Origin to upper left corner——StackOverflow

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Matplotlib 图表的原点移动左上角,可以通过以下步骤实现: 1. 导入所需的模块和函数: ```python from matplotlib.lines import Line2D import matplotlib.pyplot as plt ``` 2. 创建一个图表和一个轴对象: ```python figure, ax = plt.subplots() ``` 3. 设置 x 和 y 值的范围: ```python ax.set_xlim(left=0, right=20) ax.set_ylim(bottom=10, top=0) ``` 4. 将 x 坐标标记移到上方: ```python ax.xaxis.tick_top() ``` 5. 创建需要绘制的线条数据: ```python line1 = [(1, 1), (5, 5)] line2 = [(11, 9), (8, 8)] ``` 6. 将线条数据分别解压为 x 和 y 坐标的列表: ```python (line1_xs, line1_ys) = zip(*line1) (line2_xs, line2_ys) = zip(*line2) ``` 7. 在轴对象上添加线条对象: ```python ax.add_line(Line2D(line1_xs, line1_ys, linewidth=1, color='blue')) ax.add_line(Line2D(line2_xs, line2_ys, linewidth=1, color='red')) ``` 8. 显示图表: ```python plt.plot() plt.show() ``` 通过以上步骤,你可以将 Matplotlib 图表的原点移动左上角。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [matplotlib 坐标原点移到左上角](https://blog.csdn.net/qq_16912257/article/details/53887597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [matplotlib 设置左上角坐标原点](https://blog.csdn.net/waydong/article/details/78989552)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值