import pylab as plt
#generating data
mySamples= []
myLinear= []
myQuadratic= []
myCubic= []
myExponential= []
for i in range(0, 30):
mySamples.append(i)
myLinear.append(i)
myQuadratic.append(i**2)
myCubic.append(i**3)
myExponential.append(1.5**i)
#SIMPLE EXAMPLE
plt.plot(mySamples, myLinear)
#All graphs in single window
plt.plot(mySamples, myLinear)
plt.plot(mySamples, myQuadratic)
plt.plot(mySamples, myCubic)
plt.plot(mySamples, myExponential)
#To plot each graph in a seperate window
plt.figure('lin')
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.plot(mySamples, myQuadratic)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)
plt.figure('lin')
plt.xlabel('sample points') #labels x axis
plt.ylabel('linear function')
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.plot(mySamples, myQuadratic)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)
plt.figure('quad')
plt.ylabel('quadratic function')
plt.figure('lin')
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.plot(mySamples, myQuadratic)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)
plt.figure('lin')
plt.title('Linear') # adds title to the graph
plt.figure('quad')
plt.title('Quadratic')
plt.figure('cube')
plt.title('Cubic')
plt.figure('expo')
plt.title('Exponential')
plt.figure('lin')
plt.clf() # helps clear and start new version , as you can see axis lable is missing
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.clf()
plt.plot(mySamples, myQuadratic)
plt.figure('cube')
plt.clf()
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.clf()
plt.plot(mySamples, myExponential)
plt.figure('lin')
plt.title('Linear')
plt.figure('quad')
plt.title('Quadratic')
plt.figure('cube')
plt.title('Cubic')
plt.figure('expo')
plt.title('Exponential')
plt.figure('lin')
plt.clf()
plt.ylim(0,1000) # limits the axis
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.clf()
plt.ylim(0,1000)
plt.plot(mySamples, myQuadratic)
plt.figure('lin')
plt.title('Linear')
plt.figure('quad')
plt.title('Quadratic')
plt.figure('linquad')
plt.clf()
plt.plot(mySamples, myLinear)
plt.plot(mySamples, myQuadratic)
plt.figure('cube exp')
plt.clf()
plt.plot(mySamples, myCubic)
plt.plot(mySamples, myExponential)
plt.figure('linquad')
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.title('Cubic vs. Exponential')
plt.figure('linquad')
plt.clf()
plt.plot(mySamples, myLinear, label = 'linear')
plt.plot(mySamples, myQuadratic, label = 'quadratic')
plt.legend(loc= 'upper left') #adding legend and specifying the location
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.clf()
plt.plot(mySamples, myCubic, label = 'cubic')
plt.plot(mySamples, myExponential, label = 'exponential')
plt.legend()
plt.title('Cubic vs. Exponential')
examples:
plt.figure('linquad')
plt.clf()
plt.plot(mySamples, myLinear, 'b-', label = 'linear') # b- blue line
plt.plot(mySamples, myQuadratic,'ro', label = 'quadratic') # ro red dotted
plt.legend(loc= 'upper left')
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.clf()
plt.plot(mySamples, myCubic, 'g^', label = 'cubic') # green trinagle
plt.plot(mySamples, myExponential, 'r--',label = 'exponential')# red dash
plt.legend()
plt.title('Cubic vs. Exponential')
plt.figure('linquad')
plt.clf()
plt.plot(mySamples, myLinear, 'b-', label = 'linear', linewidth= 2.0) #choosing line width
plt.plot(mySamples, myQuadratic,'r', label = 'quadrati', linewidth= 3.0)
plt.legend(loc= 'upper left')
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.clf()
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth= 4.0)
plt.plot(mySamples, myExponential, 'r',label= 'exponential', linewidth= 5.0)
plt.legend()
plt.title('Cubic vs. Exponential')
plt.figure('linquad')
plt.clf()
plt.subplot(211) #subplots
plt.ylim(0,900)
plt.plot(mySamples, myLinear, 'b-', label = 'linear', linewidth= 2.0)
plt.subplot(212)
plt.ylim(0,900)
plt.plot(mySamples, myQuadratic,'r', label = 'quadratic', linewidth= 3.0)
plt.legend(loc= 'upper left')
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.clf()
plt.subplot(121)
plt.ylim(0, 140000)
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth= 4.0)
plt.subplot(122)
plt.ylim(0, 140000)
plt.plot(mySamples, myExponential, 'r',label= 'exponential', linewidth= 5.0)
plt.legend()
plt.title('Cubic vs. Exponential')
plt.figure('cube explog')
plt.clf()
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth= 2.0)
plt.plot(mySamples, myExponential, 'r',label= 'exponential', linewidth= 4.0)
plt.yscale('log') # ploting on log scale on y axis
plt.legend()
plt.title('Cubic vs. Exponential')
plt.figure('cube explinear')
plt.clf()
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth= 2.0)
plt.plot(mySamples, myExponential, 'r',label= 'exponential', linewidth= 4.0)
plt.legend()
plt.title('Cubic vs. Exponential')
Reference