matplotlib is an excellent 2D (and 3D) python library that can be used to produce publication quality output from your data. The website https://matplotlib.org/ provides a complete resource for how to use matplotlib for your work. In particular if you click on an example plot in the gallery, https://matplotlib.org/gallery/index.html, the browser will display the code required to produce the plot. It is quite difficult to ask google "I would like my plot to look like this, and have these features, how do I do it?", however it is easy to browse through the gallery until you see the feature that you are interested in.
Unlike software like Excel in matplotlib you write code to determine the appearance of all aspects of your graph, you can recycle this code to easily create reproducable, consistent publication quality representations of your scientific data
Preparing the notebook for using matplotlib and numpy.
%matplotlib inline
# this line is required for the plots to appear in the Jupyter cells, rather than launching the matplotlib GUI
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
# Let printing work the same in Python 2 and 3
from __future__ import print_function
# notice two underscores _ either side of future
x=np.linspace(0,2*np.pi, 100)
y=np.cos(x)
plt.figure()
plt.plot(x,y)
plt.figure()
plt.plot(x,y,'r+') # change the line style to red plusses highlights that we are dealing with a discrete set of points
print(dir(plt)) # matplotlib.pyplot is an extensive package
help(plt.plot) # the plot docstring gives a detailed set of instructions on the usasge
plot(*args, **kwargs) refers to the functions arguments and keyword arguments. The order of the arguments in a python function determines how the argument is passed into the function i.e plot(x,y) will have x as the x-axis, plot(y,x) will have y as the x-axis. The kwargs can come in any order as they are recognised by the keyword i.e. label='my experimental data'.
The following code begins to show how much control you can have over the appearance of the plot, in particular note that LaTex math symbols have been used to label the xticks, and the ticks have been moved to user defined positions.
z=np.sin(x)
plt.figure()
plt.plot(x,y, label=r'$cos(x)$')
plt.plot(x,z, label=r'$sin(x)$')# I have not specified the colour, but matplotlib will increment
#through a range as new plots are addded.
plt.legend(loc=1) # places the legend (created from the plot labels) in the upper-right
plt.title('My First Plot')
plt.xlabel(r'$\theta$') # the r tells python to read all characters, otherwise it would not read the \
plt.ylabel('y')
xmin,xmax=plt.xlim() # returns the current limits
plt.xlim(0,xmax*1.3) # sets new limits, makes some space on the right for the legend
plt.xticks((0,np.pi/2,np.pi,3*np.pi/2,2*np.pi),('0','$\pi/2$','$\pi$','$3\pi/2$','$2\pi$')) # Move the tick labels and use
#Latex commands for the labels
plt.tight_layout() #Ensures nothing overlaps
plt.show() # this is not needed in the notebook but is required from your code.
Data downloaded from https://climate.nasa.gov/system/internal_resources/details/original/647_Global_Temperature_Data_File.txt
climate=np.loadtxt('https://climate.nasa.gov/system/internal_resources/details/original/647_Global_Temperature_Data_File.txt') # data downloaded from the NASA climate change website.
np.shape(climate) # I want the first two columns in this array
year,tempchange=climate.transpose()[0],climate.transpose()[1]
# by sepearting the variables with a comma we can assign both in a single line
plt.figure()
plt.scatter(year,tempchange, label='NASA data')
plt.title('Nasa Climate Change data since 1880')
plt.xlabel('Year')
plt.ylabel('$\delta T$ from the 1951-1980 mean [C]')
plt.legend()
plt.show()
As a quick look at how simple it can be to analyse your data with python the following histogram can be generated with a single additional line of code.
plt.hist(tempchange)