【问题标题】:fix the range of axis values in plot修复绘图中轴值的范围
【发布时间】:2021-11-26 12:13:53
【问题描述】:

我正在绘制准确度和损失数据。 我得到了下面的图,它代表 0 之后的 3 个地方,比如 0.XXX 我的目标是只展示一个,比如 0.X

我该如何解决这个问题

acc = history.history['accuracy']
acc_val = history.history['val_accuracy']
epochs = range(len(acc))
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()

accuracy figure

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    试试下面的:

    import matplotlib.pyplot as plt
    from matplotlib.ticker import FormatStrFormatter
    
    acc = history.history['accuracy']
    
    fig, ax = plt.subplots()
    plt.plot([i for i in range(len(acc))], acc)
    ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
    plt.show()
    

    但您可能会看到具有相同值的多个刻度。或者或另外,您可以使用以下内容将刻度限制为您想要显示的内容:

    ax.set_yticks([0.8, 0.9, 1.0])
    

    【讨论】:

    • 我尝试了两种方法,但图表中没有任何变化
    • 抱歉,有那么一瞬间我对到底是什么问题感到困惑。我编辑了我的答案,这将解决您的问题。
    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    相关资源
    最近更新 更多