Python-matplotlib-解决隐藏坐标轴原有的图像不正常显示的问题


主要问题:根据代码隐藏坐标轴,此时原有的图像也被隐藏不正常显示

原代码:

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,
                 edgecolors='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

  

 

执行结果:坐标轴没有隐藏,原有的图形没有正常显示

解决方法:

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,
                 edgecolors='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

  主要修改两个地方:

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)

 

2.将隐藏坐标轴的代码移到plt.show上一行

这两个步骤缺一不可。

最终结果:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM