evenjc

[Python] Matplotlib - A Simple Plot

Feb 8th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. # Define the encoding of this script (unicode)
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Importing the module numpy, and renaming it np
  5. import numpy as np
  6.  
  7. # Importing the module matplotlib.pyplot, and renaming it plt
  8. import matplotlib.pyplot as plt
  9.  
  10.  
  11.  
  12. # Loads data from file into variables
  13. # One variable for each column, this is possible thanks to unpack=True
  14. # It is important that the number of columns corresponds with the number of variables
  15. # Note that we use skiprows=1, this is because the top row is a header
  16. # NOTE! You can download the file example_data.txt from the following URL:
  17. # folk.ntnu.no/evenjc/python/example_data.txt
  18. Pa, C, Rh, NH3, CO, NO2 = np.loadtxt('example_data.txt', unpack=True, skiprows=1)
  19.  
  20. # Plotting NH3, with the name "NH3", pink color, and a line  width of 5 points
  21. plt.plot(NH3, label="NH3", color="pink", lineWidth=5)
  22.  
  23. # Plotting CO without a name, with standard color and line width. Matplotlib will chose colors
  24. plt.plot(CO)
  25.  
  26.  
  27. # Places a legend. 8 is center  bottom. Legend uses the label-data. See NH3 plot
  28. # Do a google search for "matplotlib legend" for alternative placements
  29. # If loc = 8 is removed, matplotlib will try to find the most suitable location
  30. plt.legend(loc=8)
  31.  
  32. # Add grid to plot window
  33. plt.grid()
  34.  
  35. # Redefine the bounds of your plot
  36. plt.ylim(10, 30)
  37. plt.xlim(0, 20)
  38.  
  39. # Show plot
  40. plt.show()
  41.  
  42. # If you want to save your figure to file, use savefig instead of show.
  43. # https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html
Add Comment
Please, Sign In to add comment