Python: Introduction to Data Visualization
The programs used in this tutorial are contained in DataVisualization.zip.
The programs used to teach the basics of plotting with Python are included in the Basics section below.
Programs that are used to illustrate more advanced concepts are linked here:
- 3D Contour Plot
- 3D Radial Plot
- 3D Surface Plot
- Spline Fitting Interpolation
- Linear Regression
- Lotka-Volterra Differential Equation Solver 1, Solver 2
- Coupled Spring Mass System
- Lotka-Volterra Interactive Differential Equation Solver
Basics
- 01.py
from matplotlib.pyplot import figure, show def main(): #This first program will create a figure with nothing in it. figure() show() if __name__ == "__main__": main() - 02.py
from matplotlib.pyplot import figure, show, plot def main(): #Building on the previous program, plot a line figure() plot([4,5,6]) show() if __name__ == "__main__": main() - 03.py
from matplotlib.pyplot import figure, show, plot def main(): #Building on the previous program, plot a non-straight line figure() xdata=[1,2,3,4,5] ydata=[2,4,8,16,32] plot(xdata,ydata) show() if __name__ == "__main__": main() - 04.py
from matplotlib.pyplot import figure, show, plot, grid def main(): #Building on the previous program, plot a non-straight line figure() xdata=[1,2,3,4,5] ydata=[2,4,8,16,32] plot(xdata,ydata) show() if __name__ == "__main__": main() - 05.py
from matplotlib.pyplot import figure, show, plot, grid def main(): #Building on the previous program, plot a non-straight line and add a grid to the figure figure() xdata=[1,2,3,4,5] ydata=[10,5,30,10,12] plot(xdata,ydata) grid() show() if __name__ == "__main__": main() - 06.py
from matplotlib.pyplot import figure, show, plot, grid from numpy import linspace, sin def main(): #Building on the previous program, plot a sin wave figure() xdata=linspace(0,10,100) sindata=sin(xdata) plot(xdata,sindata) grid() show() if __name__ == "__main__": main() - 07.py
from matplotlib.pyplot import figure, show, plot, grid, xlabel, ylabel, title from numpy import linspace, sin def main(): #Building on the previous program, add titles and labels figure() xdata=linspace(0,20,200) sindata=sin(xdata) plot(xdata,sindata) grid() xlabel("x") ylabel("sin(x)") title("sin(x) plotted for many values of x") show() if __name__ == "__main__": main() - 08.py
from matplotlib.pyplot import figure, show, plot, grid, xlabel, ylabel, title from numpy import linspace, sin, cos def main(): #Building on the previous program, add a second plot and change the labels and title figure() xdata=linspace(0,10,100) sindata=sin(xdata) cosdata=cos(xdata) plot(xdata,sindata) grid() xlabel("x") ylabel("sin(x)") title("sin(x) plotted for many values of x") plot(xdata,cosdata) ylabel("cos(x)") title("cos(x) plotted for many values of x") show() if __name__ == "__main__": main() - 09.py
from matplotlib.pyplot import figure, show, plot, grid, xlabel, ylabel, title, cla from numpy import linspace, sin, cos def main(): #Building on the previous program, remove the first plot with cla() figure() xdata=linspace(0,10,100) sindata=sin(xdata) cosdata=cos(xdata) plot(xdata,sindata) grid() xlabel("x") ylabel("sin(x)") title("sin(x) plotted for many values of x") #cla() clears the current axis (only sin() is plotted there now) #cla() is most useful in a interactive session cla() plot(xdata,cosdata) xlabel("x") ylabel("cos(x)") title("cos(x) plotted for many values of x") show() if __name__ == "__main__": main() - 10.py
from matplotlib.pyplot import figure, show, plot, grid, xlabel, ylabel, title from numpy import linspace, sin, cos def main(): #Building on the previous program, plot sin() and cos() in different places fig=figure() #Generate all the data that will be plotted xdata=linspace(0,10,100) sindata=sin(xdata) cosdata=cos(xdata) #Make a subplot for sin(x) fig.add_subplot(2,1,1) plot(xdata,sindata) grid() xlabel("x") ylabel("sin(x)") title("sin(x) plotted for many values of x") #Make a subplot for cos(x) fig.add_subplot(2,1,2) plot(xdata,cosdata) grid() xlabel("x") ylabel("cos(x)") title("cos(x) plotted for many values of x") show() if __name__ == "__main__": main() - 11.py
from matplotlib.pyplot import figure, show, plot, grid, xlabel, ylabel, title from numpy import linspace, sin, cos def main(): #Building on the previous program, plot sin() and cos() in different places fig=figure() #Generate all the data that will be plotted xdata=linspace(0,10,100) sindata=sin(xdata) cosdata=cos(xdata) #Make a subplot for sin(x) fig.add_subplot(1,2,1) plot(xdata,sindata) grid() xlabel("x") ylabel("sin(x)") title("sin(x) plotted for many values of x") #Make a subplot for cos(x) fig.add_subplot(1,2,2) plot(xdata,cosdata) grid() xlabel("x") ylabel("cos(x)") title("cos(x) plotted for many values of x") show() if __name__ == "__main__": main() - 12_Scatter.py
from matplotlib.pyplot import figure, show, scatter def main(): fig=figure() #scatter plot two points - one at (1,10), the other at (2,5) #scatter() requires two arguments - a list of x coordinates and a list of y coordinates scatter([1,2], [10,5]) show() if __name__ == "__main__": main() - 12_Scatter_error.py
from matplotlib.pyplot import figure, show, scatter def main(): fig=figure() #scatter plot two points - one at (1,10), the other at (2,5) #scatter() requires two arguments - a list of x coordinates and a list of y coordinates scatter([1,2], [10,5,10]) show() if __name__ == "__main__": main() - 13_Scatter.py
from matplotlib.pyplot import figure, show, scatter from numpy import random def main(): #Scatter a bunch of points taken from a Gaussian (normal) distribution coords=random.randn(1000,2) x=coords[:,0] y=coords[:,1] fig=figure() scatter(x,y) show() if __name__ == "__main__": main() - 13_Scatter_alternate.py
from matplotlib.pyplot import figure, show, scatter from numpy import random, loadtxt def main(): #Scatter a bunch of points taken from a Gaussian (normal) distribution coords=loadtxt("normaldata.txt") x=coords[:,0] y=coords[:,1] fig=figure() scatter(x,y) show() if __name__ == "__main__": main() - 14_Scatter.py
from matplotlib.pyplot import figure, show, scatter, hist from numpy import random def main(): #Scatter a bunch of points taken from a Gaussian (normal) distribution, and plot histograms for x and y data coords=random.randn(1000,2) x=coords[:,0] y=coords[:,1] fig=figure() #Each of the "add_subplot" calls make a different axis todraw things on. fig.add_subplot(3,1,1) scatter(x,y) fig.add_subplot(3,1,2) hist(x, bins=20) fig.add_subplot(3,1,3) hist(y, bins=20) show() if __name__ == "__main__": main() - 15_Scatter.py
from matplotlib.pyplot import axes, scatter, hist, axis, figure, show from numpy import random def main(): #Build on the previous program and move the histograms to where they make sense coords=random.randn(1000,2) x=coords[:,0] y=coords[:,1] fig=figure() #How to create an axes: axes([left, bottom, width, height]) #These three axes should not overlap - each one will plot something different. ax=axes([.05,.25,.7,.7]) ax1=axes([.05,.05,.7,.15]) ax2=axes([.8,.25,.15,.7]) #Make a scatter plot, and then plot histograms for each dimension of data. ax.scatter(x,y) ax1.hist(x,bins=20) ax2.hist(y,bins=20, orientation="horizontal") show() if __name__ == "__main__": main() - 16_Scatter.py
from pylab import axes, scatter, hist, axis, figure, show from numpy import random def main(): """ Plot a normal and uniform set of synthetic data. """ uniformplot() normalplot() show() def uniformplot(): #First, create a bunch of "experimental data" coords1=random.randint(0,1000,size=(1000,2)) #Make a figure that we can plot on. fig=figure() #How to create an axes: axes([left, bottom, width, height]) #These three axes should not overlap - each one will plot something different. ax=axes([.05,.25,.7,.7]) ax1=axes([.05,.05,.7,.15]) ax2=axes([.8,.25,.15,.7]) x=coords1[:,0] y=coords1[:,1] #Make a scatter plot, and then plot histograms for each dimension of data. ax.scatter(x,y) ax1.hist(x,bins=20) ax2.hist(y,bins=20, orientation="horizontal") #Reset the scatter plot axis limits to "fit" ax.axis([0,1000,0,1000]) def normalplot(): coords2=random.randn(1000,2) fig=figure() bx=axes([.05,.25,.7,.7]) bx1=axes([.05,.05,.7,.15]) bx2=axes([.8,.25,.15,.7]) bx.scatter(coords2[:,0],coords2[:,1]) bx1.hist(coords2[:,0],bins=20) bx2.hist(coords2[:,1],bins=20, orientation="horizontal") if __name__ == "__main__": main() - 17_Pie.py
from matplotlib.pyplot import figure, show, pie def main(): #Make a basic pie chart figure() piedata=[10,20,30] pie(piedata) show() if __name__ == "__main__": main() - 18_Pie.py
from matplotlib.pyplot import figure, show, pie def main(): #Build on the previous program and add labels to the pie pieces figure() piedata=[10,20,30] pie(piedata, labels=["First", "Second", "Third"]) show() if __name__ == "__main__": main() - 18_Pie_error.py
from matplotlib.pyplot import figure, show, pie def main(): #Build on the previous program and add labels to the pie pieces figure() piedata=[10,20,30] pie(piedata, labels=["First", "Second"]) show() if __name__ == "__main__": main()
