zoukankan      html  css  js  c++  java
  • 在matplotlib中关闭绘图轴的方法

    我正在为随机游走模型编写代码,我使用plt.axes().get_xaxis().set_visible(False)来隐藏轴,但是当我运行程序时,绘图仍然显示两个轴。以下是我写的:

    import matplotlib.pyplot as plt 
    from random_walk import RandomWalk
    
    # Keep making random walks, as long as the program is active
    while True:
        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=10)
    
        # Emphasize the first and last points.
        plt.scatter(0, 0, c='green', edgecolors='none', s=50)
        plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', s=50)
    
        # Remove the axes.
        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 

    你可以用plt.gca().get_xaxis().set_visible(False)代替plt.axes().get_xaxis().set_visible(False)

    下面是一个代码示例:

    import matplotlib.pyplot as plt
    from random_walk import RandomWalk
    # 解决不能显示中文
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    while True:
    # 创建一个RandomWalk实例,并绘制
        rw=RandomWalk()
        rw.fill_walk()
        points_numbers=list(range(rw.num_points))
        # 设置图表标题,并给坐标轴加上标签
        plt.title("随机漫步图", fontsize=24)
        plt.scatter(rw.x_values,rw.y_values,c=points_numbers,cmap=plt.cm.Blues,edgecolors='none',s=15)
        # 隐藏坐标轴
        plt.gca().get_xaxis().set_visible(False)
        plt.gca().get_yaxis().set_visible(False)
        #plt.show()
        plt.savefig('sjmb.png',bbox_inches='tight')
        keep_running=input("是否要再模拟一次随机漫步?(y/n):")
        if keep_running=='n':
            break
  • 相关阅读:
    应用实例-最大子列和问题
    什么是算法
    什么是数据结构
    数据结构与算法-Python/C(目录)
    collections模块
    色彩缤纷的Python(改变字体颜色及样式)
    取数组中一段值的算法(转)
    开发者必备,超实用的PHP代码片段(转)
    php中数组自定义排序
    rand值出现负数的解决方案
  • 原文地址:https://www.cnblogs.com/HGNET/p/15091251.html
Copyright © 2011-2022 走看看