为什么ylim对我的情节没有理想的效果?

时间:2019-04-07 14:52:03

标签: python pandas matplotlib

ylim参数在精度图上没有理想的效果。在axis [0]上调用set_ylim确实具有相同的效果。

我的代码如下:

fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10,8))
axes[0].plot(df['Acc'], "g--", label="Accuracy of training data")
axes[0].plot(df['Val_acc'], "g", label="Accuracy of validation data")
axes[0].set(title="Accuracy",
            ylabel="Accuracy",
            xlabel="Epochs",
            ylim=[0,1],
            xlim=[0, len(df)-1])
axes[0].legend(loc="best")
axes[1].plot(df['Loss'], "r--", label="Loss of training data")
axes[1].plot(df['Val_loss'], "r", label="Loss of validation data")
axes[1].set(title="Loss",
            ylabel="Loss",
            xlabel="Epochs",
            xlim=[0, len(df)-1])
axes[1].legend(loc="best")
fig.tight_layout()
fig.savefig('model4.png', dpi=300)

Faulty_plot

有人知道如何解决此问题吗?

编辑:试图提出一个简短的,可重现的ylim实例(并且@Sheldore指出,损失图的值不再被打乱)。

test_index = np.arange(1,11)
test_columns = ['Acc', 'Loss', 'Val_loss', 'Val_acc']
df_test = pd.DataFrame(index=test_index, columns=test_columns)
df_test['Acc'] = np.array([0.5007, 0.4984, 0.4991, 0.4982, 0.4941, 0.5043, 0.4971, 0.4975, 0.4966, 0.4992])
df_test['Loss'] = np.array([0.6940, 0.6937, 0.6933, 0.6932, 0.6947, 0.6932, 0.6932, 0.6932, 0.6932, 0.6932])
df_test['Val_loss'] = np.array([0.6934, 0.6931, 0.6932, 0.6931, 0.6929, 0.6931, 0.6932, 0.6932, 0.6931, 0.6934])
df_test['Val_acc'] = np.array([0.4935, 0.5065, 0.4935, 0.5112, 0.5017, 0.5065, 0.4935, 0.4935, 0.4935, 0.4935])
df_test.head()



fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10,8))
axes[0].plot(df_test['Acc'], "g--", label="Accuracy of training data")
axes[0].plot(df_test['Val_acc'], "g", label="Accuracy of validation data")
axes[0].set(title="Accuracy",
            ylabel="Accuracy",
            xlabel="Epochs",
            ylim=[0,1],
            xlim=[1, len(df_test)-1])
#axes[0].set_ylim([0,1])
axes[0].legend(loc="best")
axes[1].plot(df_test['Loss'], "r--", label="Loss of training data")
axes[1].plot(df_test['Val_loss'], "r", label="Loss of validation data")
axes[1].set(title="Loss",
            ylabel="Loss",
            xlabel="Epochs",
            xlim=[1, len(df_test)-1])
axes[1].legend(loc="best")
fig.tight_layout()
fig.savefig('test_model.png', dpi=300)

Test model from minimal example

0 个答案:

没有答案