Matplotlib中使用plt.axes()隐藏坐标轴

一、问题简述

在进行“数据可视化”这个python入门项目之时,本想实现只显示图像而隐藏坐标轴的功能,但遇到了按照教程操作却无法隐藏坐标轴反而隐藏了图像的问题。

二、初始代码及结果

问题代码如下:

import matplotlib.pyplot as plt

from random_walk import RandomWalk

while True:
    #创建一个RandomWalk实例,将其包含点绘制出来
    rw=RandomWalk()
    rw.fill_walk()
    
	#给点着色
    point_numbers=list(range(rw.num_points))
    plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolor='none',s=15)

    #突出起点和终点
    plt.scatter(0,0,c='green',edgecolors='none',s=100)
    plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)

    #隐藏坐标轴
    plt.axes().get_xaxis().set_visible(False)
    plt.axes().get_yaxis().set_visible(False)

    plt.show()

    keep_running=input("Make another walk?(y/n):")
    if keep_running=='n':
        break

问题结果输出如下:
在这里插入图片描述
可以看出存在问题:

  • 坐标轴部分数据重叠错乱
  • 坐标轴未隐藏
  • 隐藏了图像

三、修改代码及结果

最终修改代码如下:

import matplotlib.pyplot as plt

from random_walk import RandomWalk

while True:
    #创建一个RandomWalk实例,将其包含点绘制出来
    rw=RandomWalk()
    rw.fill_walk()

    #隐藏坐标轴
    # plt.axes().get_xaxis().set_visible(False)
    # plt.axes().get_yaxis().set_visible(False)
    current_axes=plt.axes()
    current_axes.xaxis.set_visible(False)
    current_axes.yaxis.set_visible(False)

	#给点着色
    point_numbers=list(range(rw.num_points))
	plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolor='none',s=15)
    
    #突出起点和终点
    plt.scatter(0,0,c='green',edgecolors='none',s=100)
    plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)

    plt.show()

    keep_running=input("Make another walk?(y/n):")
    if keep_running=='n':
        break

正确运行结果如下:
在这里插入图片描述

四、具体修改内容

4.1 第一部分

    #隐藏坐标轴
    # plt.axes().get_xaxis().set_visible(False)
    # plt.axes().get_yaxis().set_visible(False)
    current_axes=plt.axes()
    current_axes.xaxis.set_visible(False)
    current_axes.yaxis.set_visible(False)

对这一部分代码修改原因可参考此处链接

输出结果:
在这里插入图片描述
可以看出,虽然解决了坐标轴重叠错乱问题,但仍未能显示图像和隐藏坐标轴。

4.2 第二部分

将“隐藏坐标轴”模块移动到“给点着色”模块之前:

while True:
    #创建一个RandomWalk实例,将其包含点绘制出来
    rw=RandomWalk()
    rw.fill_walk()

    #隐藏坐标轴
    # plt.axes().get_xaxis().set_visible(False)
    # plt.axes().get_yaxis().set_visible(False)
    current_axes=plt.axes()
    current_axes.xaxis.set_visible(False)
    current_axes.yaxis.set_visible(False)

    #给点着色
    point_numbers=list(range(rw.num_points))
    plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolor='none',s=15)

    #突出起点和终点
    plt.scatter(0,0,c='green',edgecolors='none',s=100)
    plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)

    plt.show()

    keep_running=input("Make another walk?(y/n):")
    if keep_running=='n':
        break

输出结果:
在这里插入图片描述
将“隐藏坐标轴”模块移动到“着色”模块之前,便可成功隐藏坐标轴,仅显示图像。

只有完成了这两部分的修改,显示结果才会既隐藏坐标轴又显示图像。

五、隐藏边框

还可再进一步,在plt.show()语句之前加上plt.axis(‘off’)语句,便可得到无坐标轴无边框的图像。显示图像如下:
在这里插入图片描述

And now I see. With eye serene. The very pulse of the machine.

  • 15
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
使用plt.imshow函数时,可以通过两种方式来反转坐标轴。第一种方式是使用ax.invert_yaxis()方法,该方法可以在创建图像的Axes对象后调用,例如: ``` fig = plt.figure(figsize=(15,10)) ax = fig.add_subplot(1,1,1) ax.invert_yaxis() ``` 这样可以将y轴进行反转。第二种方式是在plt.imshow函数传递参数,具体可以使用matplotlib.axes.Axes对象来设置参数,例如: ``` plt.imshow(image, extent=\[xmin, xmax, ymin, ymax\], origin='upper') ``` 其,origin参数设置为'upper'可以将y轴进行反转。这两种方式都可以实现反转坐标轴的效果。\[1\] \[2\] #### 引用[.reference_title] - *1* [matplotlib画图时y轴反向](https://blog.csdn.net/Blankit1/article/details/127028717)[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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python matplotlib.axes相关属性设置(绘图方式、坐标轴、坐标刻度、文本等)](https://blog.csdn.net/weixin_44237337/article/details/116149154)[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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值