1

I am trying to plot 3 loss curves in one figure. I have the following problems.

  1. I can't get a smooth curve, instead it joins point-to-point in a sharp line

  2. How do I change the scales of the axis, to display the MSE loss which disappears because it is too small?

from matplotlib import pyplot as plt

epochs=list(range(5000,50001,5000))
print(epochs)
mae_loss=[0.500225365,
0.000221096,
0.000060971,
0.000060323,
0.000059905,
0.000059579,
0.000059274,
0.000058972,
0.000058697,
0.000058476]

mse_loss=[0.135419831,
0.018331185,
0.002481434,
0.000335913,
0.000045486,
0.000006180,
0.000000867,
0.000000147,
0.000000042,
0.000000042]

rmse_loss=[0.500225306,
0.000293739,
0.000126985,
0.000121944,
0.000119484,
0.000117791,
0.000116400,
0.000115198,
0.000114148,
0.000113228]

plt.plot(epochs, mae_loss, 'b', label='MAE')
plt.plot(epochs, mse_loss, 'r', label='MSE')
plt.plot(epochs, mse_loss, 'g', label='RMSE')
plt.legend()
plt.show()

2 Answers 2

3

You would require some interpolation methods to get a smooth spline/curve. That is in itself a different question. I will answer the question regarding the different scales. Since the order of magnitudes of your data are quite different, in such situations, the best solution is to use a logarithmic y-scale using semilogy. P.S: You had written mse_loss instead of rmse_loss in the last plot line.

plt.semilogy(epochs, mae_loss, 'b', label='MAE')
plt.semilogy(epochs, mse_loss, 'r', label='MSE')
plt.semilogy(epochs, rmse_loss, 'g', label='RMSE')
plt.legend()
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

To smooth your plots:

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import make_interp_spline, BSpline

def create_spline_from(x, y, resolution):
  new_x = np.linspace(x[0], x[-1], resolution)
  y_spline = make_interp_spline(x, y, k=3)
  new_y= y_spline(new_x)

  return (new_x, new_y)

epochs=list(range(5000,50001,5000))
print(epochs)
mae_loss=[0.500225365,
0.000221096,
0.000060971,
0.000060323,
0.000059905,
0.000059579,
0.000059274,
0.000058972,
0.000058697,
0.000058476]

mse_loss=[0.135419831,
0.018331185,
0.002481434,
0.000335913,
0.000045486,
0.000006180,
0.000000867,
0.000000147,
0.000000042,
0.000000042]

rmse_loss=[0.500225306,
0.000293739,
0.000126985,
0.000121944,
0.000119484,
0.000117791,
0.000116400,
0.000115198,
0.000114148,
0.000113228]

x, y = create_spline_from(epochs, mae_loss, 50)
plt.plot(x, y, 'b', label='MAE')

x, y = create_spline_from(epochs, mse_loss, 50)
plt.plot(x, y, 'r', label='MSE')

x, y = create_spline_from(epochs, rmse_loss, 50)
plt.plot(x, y, 'g', label='RMSE')
plt.legend()
plt.show()

2 Comments

Thanks, however there appears to be a dip in the lines. Why does that happen?
Thats the nature of smoothing/splines. Notices that smoothing data in this case really means making up data points in between the actual data points. That data does not exist/is fake. The fake data purely exists to provide a smooth plot and one must use some algorithm to pick that fake data to create 'smooth curves'. I used the bspline from scipy. Better is to take more data points naturally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.