The first code block trains the model, while the second one only appends the loss to temp_loss_list.
I’m not sure why you would need the second code block, since you are also using a validation loop (which is correct).
Regarding the weird plot output: I would suggest to check the shape of the array you would like to plot, as it seems as matplotlib might interpret the input array differently than what you expect.
In particular the shapes of X_train and score would be interesting to see.
PS: You shouldn’t use the .data attribute, as it might yield unwanted side effects. It should be fine in your code snippet, but I would generally advice to skip the .data usage completely.