我如何在matplotlib中设置y轴限制
How can i set the y axis limit in matplotlib
我正在比较两个不同神经网络的训练精度。我如何设置比例以使它们具有可比性。 (比如将两个 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
两张图如下:
尝试使用 matplotlib.pyplot.ylim(低,高)参考这个 link https://www.geeksforgeeks.org/matplotlib-pyplot-ylim-in-python/
您可以使用 fig, ax = plt.subplots(1, 2)
构建 1 行 2 列的子图 (reference)。
基本代码
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()
我正在比较两个不同神经网络的训练精度。我如何设置比例以使它们具有可比性。 (比如将两个 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
两张图如下:
尝试使用 matplotlib.pyplot.ylim(低,高)参考这个 link https://www.geeksforgeeks.org/matplotlib-pyplot-ylim-in-python/
您可以使用 fig, ax = plt.subplots(1, 2)
构建 1 行 2 列的子图 (reference)。
基本代码
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()