如何设置y轴限制? [重复]

问题描述 投票:0回答:2

我正在比较两种不同神经网络的训练精度。我怎样才能设置比例以使它们具有可比性。 (就像将两个 y 轴都设置为 1 以便图表具有可比性)

我使用的代码如下:

 def NeuralNetwork(X_train, Y_train, X_val, Y_val, epochs, nodes, lr):
        hidden_layers = len(nodes) - 1
        weights = InitializeWeights(nodes)
        Training_accuracy=[]
        Validation_accuracy=[]
        for epoch in range(1, epochs+1):
            weights  = Train(X_train, Y_train, lr, weights)
    
            if (epoch % 1 == 0):
                print("Epoch {}".format(epoch))
                print("Training Accuracy:{}".format(Accuracy(X_train, Y_train, weights)))
                
                if X_val.any():
                    print("Validation Accuracy:{}".format(Accuracy(X_val, Y_val, weights)))
                Training_accuracy.append(Accuracy(X_train, Y_train, weights))
                Validation_accuracy.append(Accuracy(X_val, Y_val, weights))
        plt.plot(Training_accuracy) 
        plt.plot((Validation_accuracy),'#008000') 
        plt.legend(["Training_accuracy", "Validation_accuracy"])    
        plt.xlabel("Epoch")
        plt.ylabel("Accuracy")  
        return weights , Training_accuracy , Validation_accuracy

两张图如下:

python matplotlib visualization subplot yaxis
2个回答
4
投票

您可以使用

fig, ax = plt.subplots(1, 2)
构建一个 1 行 2 列的子图(参考)。

基本代码

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(16)
a = np.random.rand(10)
b = np.random.rand(10)

fig, ax = plt.subplots(1, 2)

ax[0].plot(a)
ax[1].plot(b)

plt.show()

如果您设置

sharey = 'all'
参数,您创建的所有子图将共享相同的 y 轴比例:

fig, ax = plt.subplots(1, 2, sharey = 'all')

最后,您可以使用

ax[0].set_ylim(0, 1)
手动设置 y 轴的限制。

完整代码

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(16)
a = np.random.rand(10)
b = np.random.rand(10)

fig, ax = plt.subplots(1, 2, sharey = 'all')

ax[0].plot(a)
ax[1].plot(b)

ax[0].set_ylim(0, 1)

plt.show()


2
投票

尝试使用 matplotlib.pyplot.ylim(low, high) 请参阅此链接 https://www.geeksforgeeks.org/matplotlib-pyplot-ylim-in-python/

© www.soinside.com 2019 - 2024. All rights reserved.