QF205 Lecture 6 Answers
Question 1
from numpy import *
from matplotlib import pyplot
def plotsin(a,b):
t=arange(a,b,0.1)
pyplot.figure(1)
pyplot.plot(t,sin(t),'k')
pyplot.show()
plotsin(0,2)
Question 2 (Prof, Pls Check. The color is not exactly the same as shown in the question)
from numpy import *
from matplotlib import pyplot
fig=pyplot.figure()
ax=fig.add_subplot(111)
p11=pyplot.bar(-0.5,1,1,0,color='#151B8D')
p12=pyplot.bar(0.5,1,1,0,color='#3BB9FF')
p13=pyplot.bar(1.5,1,1,0,color='#151B54')
p21=pyplot.bar(-0.5,1,1,1,color='#2554C7')
p22=pyplot.bar(0.5,1,1,1,color='#3090C7')
p23=pyplot.bar(1.5,1,1,1,color='#FDD017')
p31=pyplot.bar(-0.5,1,1,2,color='#F88017')
p32=pyplot.bar(0.5,1,1,2,color='#6AFB92')
p33=pyplot.bar(1.5,1,1,2,color='#C11B17')
p41=pyplot.bar(-0.5,1,1,3,color='#3090C7')
p42=pyplot.bar(0.5,1,1,3,color='#F62817')
p43=pyplot.bar(1.5,1,1,3,color='r')
p51=pyplot.bar(-0.5,1,1,4,color='#C11B17')
p52=pyplot.bar(0.5,1,1,4,color='#153E7E')
p53=pyplot.bar(1.5,1,1,4,color='#E42217')
ax.set_xlim(-0.5,2.5)
pyplot.yticks(arange(0,5)+0.5,(4,3,2,1,0))
pyplot.xticks(arange(-0.5,3,0.5))
ax.autoscale_view()
pyplot.show()
Question 3A
[[1,0,0,1,0,0,0],[1,0,0,0,1,0,0],[1,0,0,0,0,1,0],[1,0,0,0,0,0,1],\
[0,1,0,1,0,0,0],[0,1,0,0,1,0,0],[0,1,0,0,0,1,0],[0,1,0,0,0,0,1],\
[0,0,1,1,0,0,0],[0,0,1,0,1,0,0],[0,0,1,0,0,1,0],[0,0,1,0,0,0,1]]
1
QF205 Lecture 6 Answers
Question 3B
[[0,0,0,1,1,1,1],[0,0,0,1,1,1,1],[0,0,0,1,1,1,1],[1,1,1,0,0,0,0],\
[1,1,1,0,0,0,0],[1,1,1,0,0,0,0],[1,1,1,0,0,0,0]]
Question 4A
import pydot
mat =
[[1,0,0,1,0,0,0],[1,0,0,0,1,0,0],[1,0,0,0,0,1,0],[1,0,0,0,0,0,1],\
[0,1,0,1,0,0,0],[0,1,0,0,1,0,0],[0,1,0,0,0,1,0],[0,1,0,0,0,0,1],\
[0,0,1,1,0,0,0],[0,0,1,0,1,0,0],[0,0,1,0,0,1,0],[0,0,1,0,0,0,1]]
g = pydot.graph_from_incidence_matrix(mat)
subg= pydot.Subgraph('', rank='same')
subg.add_node(pydot.Node('1',label='',color='blue'))
subg.add_node(pydot.Node('2',label='',color='blue'))
subg.add_node(pydot.Node('3',label='',color='blue'))
g.add_subgraph(subg)
subg1= pydot.Subgraph('', rank='same')
subg1.add_node(pydot.Node('4',label='',color='blue'))
subg1.add_node(pydot.Node('5',label='',color='blue'))
subg1.add_node(pydot.Node('6',label='',color='blue'))
subg1.add_node(pydot.Node('7',label='',color='blue'))
g.add_subgraph(subg1)
g.write_jpeg('Excersize4A.jpg', prog='dot')
Question 4B
import pydot
mat =
[[0,0,0,1,1,1,1],[0,0,0,1,1,1,1],[0,0,0,1,1,1,1],[1,1,1,0,0,0,0],\
[1,1,1,0,0,0,0],[1,1,1,0,0,0,0],[1,1,1,0,0,0,0]]
g = pydot.graph_from_adjacency_matrix(mat)
subg= pydot.Subgraph('', rank='same')
subg.add_node(pydot.Node('1',label='',color='blue'))
subg.add_node(pydot.Node('2',label='',color='blue'))
subg.add_node(pydot.Node('3',label='',color='blue'))
g.add_subgraph(subg)
subg1= pydot.Subgraph('', rank='same')
subg1.add_node(pydot.Node('4',label='',color='blue'))
subg1.add_node(pydot.Node('5',label='',color='blue'))
2
QF205 Lecture 6 Answers
subg1.add_node(pydot.Node('6',label='',color='blue'))
subg1.add_node(pydot.Node('7',label='',color='blue'))
g.add_subgraph(subg1)
g.write_jpeg('Excersize4B.jpg', prog='dot')
Question 4C
import pydot
edges=[(1,4), (1,5), (1,6), (1,7),(2,4), (2,5), (2,6), (2,7),(3,4), \
(3,5), (3,6), (3,7)]
g=pydot.graph_from_edges(edges)
subg= pydot.Subgraph('', rank='same')
subg.add_node(pydot.Node('1',label='',color='blue'))
subg.add_node(pydot.Node('2',label='',color='blue'))
subg.add_node(pydot.Node('3',label='',color='blue'))
g.add_subgraph(subg)
subg1= pydot.Subgraph('', rank='same')
subg1.add_node(pydot.Node('4',label='',color='blue'))
subg1.add_node(pydot.Node('5',label='',color='blue'))
subg1.add_node(pydot.Node('6',label='',color='blue'))
subg1.add_node(pydot.Node('7',label='',color='blue'))
g.add_subgraph(subg1)
g.write_jpeg('Excersize4C.jpg', prog='dot')
3