Matplotlib 2D Example : Python

2D Example

  1. import numpy as np
  2. import matplotlib as mpl
  3. import matplotlib.pyplot as plt
  4.  
  5. mpl.rcParams['xtick.labelsize'] = 24
  6. mpl.rcParams['ytick.labelsize'] = 24
  7. np.random.seed(42)
  8.  
  9. x = np.linspace(0, 5, 100)
  10. y = 2 * np.sin(x) + 0.3 * x ** 2
  11. y_data = y + np.random.normal(scale=0.3, size=100)
  12. plt.figure('data')
  13. plt.plot(x, y_data, '.')
  14.  
  15. plt.figure('model')
  16. plt.plot(x, y)
  17.  
  18. plt.figure('data & model')
  19. plt.plot(x, y, 'k', lw=3)
  20. plt.scatter(x, y_data)
  21.  
  22. plt.savefig('result.png')
  23.  
  24. plt.show()



Leave a Reply

Your email address will not be published. Required fields are marked *