【问题标题】:I can't turn off the axes of my plot in matplotlib我无法在 matplotlib 中关闭我的绘图轴
【发布时间】:2021-02-15 19:49:56
【问题描述】:

我正在为随机游走模型编写代码,我使用 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 

【问题讨论】:

    标签: python matplotlib data-visualization random-walk


    【解决方案1】:

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

    这是一个代码示例:

    import matplotlib.pyplot as plt
    import numpy as np
    
    plt.plot(np.random.random(1000))
    plt.gca().get_xaxis().set_visible(False)
    

    这个短代码将返回:

    【讨论】:

    • 对了,非常感谢
    • @Pedrolopez_e 如果有帮助,请将其标记为答案。
    猜你喜欢
    • 2020-07-15
    • 1970-01-01
    • 2016-03-10
    • 2013-04-01
    • 2012-04-12
    • 2016-03-18
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多