romillyhills 14:59 on 19.01.14 293 0

recently i decided i'd try to re-write my research into python. however, whenever i look into new languages the examples on the net are always very complex; with the lines of code needed for what i want hidden in whatever problem the writer is trying to solve. so, here is the minimum required to plot a matrix in python.

as of right now; you will need python 3.2, python 2.7, numpy, matplotlib and the thousands of dependencies they have, but i won't go into that and assume that you've got it all set up.

the code:

import numpy as np

import matplotlib.pyplot as plt

a=np.array([[0,1,2],[3,4,5],[6,7,8]])

plt.subplots()

plt.pcolor(a)

plt.show()

will produces a plot like this:

however, that looks horrific, and you may want some axis labels and ticks etc.

by adding a few [self explanatory] lines we can get a very presentable plot:

import numpy as np

import matplotlib.pyplot as plt

a=np.array([[0,1,2],[3,4,5],[6,7,8]])

plt.subplots()

plt.pcolor(a, cmap=plt.cm.Blues) #makes the colour scheme blue

plt.title('plot')

plt.colorbar()

plt.gca().invert_yaxis() #puts the top of the matrix at the top

plt.gca().set_aspect('equal') #makes each matrix element square

plt.yticks((0,1.5,3), ('200','0','-200')) #puts ticks at matrix indexes

plt.xticks((0,1.5,3), ('$-\pi/2$','0', '$\pi/2$')) #uses latex for special characters

plt.xlabel(r'$\theta$')

plt.ylabel('E')

plt.show()

which produces the plot in the top image.

the code:

import numpy as np

import matplotlib.pyplot as plt

a=np.array([[0,1,2],[3,4,5],[6,7,8]])

plt.subplots()

plt.pcolor(a)

plt.show()

will produces a plot like this:

however, that looks horrific, and you may want some axis labels and ticks etc.

by adding a few [self explanatory] lines we can get a very presentable plot:

import numpy as np

import matplotlib.pyplot as plt

a=np.array([[0,1,2],[3,4,5],[6,7,8]])

plt.subplots()

plt.pcolor(a, cmap=plt.cm.Blues) #makes the colour scheme blue

plt.title('plot')

plt.colorbar()

plt.gca().invert_yaxis() #puts the top of the matrix at the top

plt.gca().set_aspect('equal') #makes each matrix element square

plt.yticks((0,1.5,3), ('200','0','-200')) #puts ticks at matrix indexes

plt.xticks((0,1.5,3), ('$-\pi/2$','0', '$\pi/2$')) #uses latex for special characters

plt.xlabel(r'$\theta$')

plt.ylabel('E')

plt.show()

which produces the plot in the top image.