matplotlib 设置左上角为坐标轴原点

Python使用matplotlib绘图时,有时需要将坐标轴的原点设置为左上角,具体方法为:

import matplotlib.pyplot as plt

x=[1,2,3,4,5,6]
y=[10,20,30,40,50,60]
plt.plot(x, y, color='red')
plt.show()

绘制出的图像如下:


x=[1,2,3,4,5,6]
y=[10,20,30,40,50,60]

plt.plot(x, y, color='red')

ax = plt.gca()                                 #获取到当前坐标轴信息
ax.xaxis.set_ticks_position('top')   #将X坐标轴移到上面
ax.invert_yaxis()                            #反转Y坐标轴

plt.show()

转换后的图像如下:







  • 17
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要将 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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值