• 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上一行

    这两个步骤缺一不可。

    最终结果:

  • 相关阅读:
    struct2 学习总结
    c++ 容器(list学习总结)
    java 网络编程(五)----TCP进阶篇上传文本文件
    java 网络编程(四)----UDP进阶篇聊天小程序
    java 网络编程(三)---TCP的基础级示例
    java 网络编程(二)----UDP基础级的示例
    java 网络编程(一)---基础知识和概念了解
    GitHub和git和repo的使用
    android studio不能预览
    关于android studio2.3和android studio3.0
  • 原文地址:https://www.cnblogs.com/cloverben/p/15386742.html
Copyright © 2020-2023  润新知