Advertisement
elena1234

How to create many plots with Matplotlib in Python

Apr 26th, 2022 (edited)
865
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. x = np.arange(1,10)
  5. y = x ** 4
  6. a = np.arange(1,5)
  7. b = a * 100
  8.  
  9. fig,axes = plt.subplots(nrows=2, ncols = 2, dpi = 200, figsize = (5,10))
  10. axes[0][0].plot(a,b)
  11. axes[1][0].plot(x,y)
  12. axes[0][0].set_xlabel("X label")
  13. axes[0][0].set_ylabel("Y label")
  14. axes[0][0].set_title("Title 1")
  15.  
  16. fig.suptitle("Figure level",fontsize = 16)
  17.  
  18. plt.tight_layout() # to solve a problem with space
  19. # fig.subplots_adjust(left =, right = , bottom = , top = , wspace = , hspace = ) # manual spacing
  20.  
  21. ############################################################
  22. '''
  23. # Adjust the figure size
  24. plt.figure(figsize=(10,5))
  25.  
  26. # Plot the histograms of X and Y next to each other
  27. plt.subplot(1,2,1)
  28. plt.hist(x = x, bins = 15)
  29. plt.title("X")
  30.  
  31. plt.subplot(1,2,2)
  32. plt.hist(x = y, bins = 15)
  33. plt.title("Y")
  34.  
  35. plt.show()
  36.  
  37. #############################################################
  38. # Plot the data
  39. plt.figure(figsize=(10,10))
  40. plt.subplot(2,2,2)
  41. plt.scatter(x = x, y = y)
  42. plt.title("Joint Distribution of X and Y")
  43.  
  44. # Plot the Marginal X Distribution
  45. plt.subplot(2,2,4)
  46. plt.hist(x = x, bins = 15)
  47. plt.title("Marginal Distribution of X")
  48.  
  49.  
  50. # Plot the Marginal Y Distribution
  51. plt.subplot(2,2,1)
  52. plt.hist(x = y, orientation = "horizontal", bins = 15)
  53. plt.title("Marginal Distribution of Y")
  54.  
  55. # Show the plots
  56. plt.show()
  57. '''
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement