0% found this document useful (0 votes)
9 views

Lecture_TWP_Python_A05_1a_2D_Graphics

The document provides an overview of creating two-dimensional graphics using the Matplotlib library in Python. It includes code examples for plotting data, building frames, and customizing visual elements. The content is structured into sections covering graphics concepts, practical implementations, and references for further reading.

Uploaded by

Nirjhar Dhang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture_TWP_Python_A05_1a_2D_Graphics

The document provides an overview of creating two-dimensional graphics using the Matplotlib library in Python. It includes code examples for plotting data, building frames, and customizing visual elements. The content is structured into sections covering graphics concepts, practical implementations, and references for further reading.

Uploaded by

Nirjhar Dhang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Together With Python

Together With Python


Computation, Visualization and Reporting
Part A-5 : 2D Graphics

Nirjhar Dhang


Version : 1.732 : 3
Created on : August 11, 2016
Last revision : January 11, 2025
Together With Python
Computation, Visualization and Reporting
Part A-5 : 2D Graphics

Nirjhar Dhang


Version : 1.732 : 3
Created on : August 11, 2016
Last revision : January 11, 2025
Overview

1 Two-dimensional Graphics

2 Two-dimensional Graphics using matplotlib

3 Building Frame

4 Summary

5 References
Two-dimensional Graphics
Two-dimensional Graphics

Matplotlib is a Python 2D plotting library which produces


publication quality figures in a variety of hardcopy formats and
interactive environments across platforms.
Matplotlib can be used in Python scripts, the Python and
IPython shells, the Jupyter notebook, web application servers
Two-dimensional Graphics using matplotlib
Two-dimensional Graphics using matplotlib

import matplotlib.pyplot as plt


plt.plot([1, 2, 3, 4])
plt.ylabel(’numbers’)
plt.show()
Two-dimensional Graphics using matplotlib
Two-dimensional Graphics using matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel(’numbers’)
plt.show()
Two-dimensional Graphics using matplotlib

import matplotlib.pyplot as plt


plt.plot([1, 2, 3, 4],[1,4,9,16])
plt.ylabel(’square of numbers’)
plt.show()
Two-dimensional Graphics using matplotlib
Two-dimensional Graphics using matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4],[1,4,9,16])
plt.ylabel(’square of numbers’)
plt.show()
Two-dimensional Graphics using matplotlib

import matplotlib.pyplot as plt


plt.plot([1, 2, 3, 4],[1,4,9,16],’ro’)
plt.ylabel(’square of numbers’)
plt.axis([0, 5, 0, 20])
plt.show()
Two-dimensional Graphics using matplotlib
Two-dimensional Graphics using matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4],[1,4,9,16],’ro’)
plt.ylabel(’square of numbers’)
plt.axis([0, 5, 0, 20])
plt.show()
Two-dimensional Graphics using matplotlib

import numpy as np
import matplotlib.pyplot as plt

def function(t):
return np.exp(-t) * np.cos(2*np.pi*t)

tvec = np.arange(0.0, 5.0, 0.05)

plt.figure(1)
plt.plot(tvec, function(tvec))

plt.show()
Two-dimensional Graphics using matplotlib
Two-dimensional Graphics using matplotlib

import numpy as np
import matplotlib.pyplot as plt

def function(t):
return np.exp(-t) * np.cos(2*np.pi*t)

tvec1 = np.arange(0.0, 5.0, 0.05)


tvec2 = np.arange(0.0, 5.0, 0.1)

plt.figure(1)
plt.plot(tvec1, function(tvec1), ’k’, tvec2, function
(tvec2), ’ro’)

plt.show()
Two-dimensional Graphics using matplotlib
Building Frame
Building Frame
import matplotlib.pyplot as plt
plt.plot([0,0],[0,4],’b’)
plt.plot([0,0],[4,8],’b’)
plt.plot([5,5],[0,4],’r’)
plt.plot([5,5],[4,8],’r’)
plt.plot([10,10],[0,4],’b’)
plt.plot([10,10],[4,8],’b’)

plt.plot([0,5],[4,4],’k’)
plt.plot([5,10],[4,4],’k’)
plt.plot([0,5],[8,8],’m’)
plt.plot([5,10],[8,8],’m’)

plt.axis([-3, 13, -3, 13])


plt.show()
Building Frame
Building Frame
import matplotlib.pyplot as plt
plt.plot([0,0],[0,4],’b’)
plt.plot([0,0],[4,8],’b’)
plt.plot([5,5],[0,4],’r’)
plt.plot([5,5],[4,8],’r’)
plt.plot([10,10],[0,4],’b’)
plt.plot([10,10],[4,8],’b’)

plt.plot([0,5],[4,4],’k’)
plt.plot([5,10],[4,4],’k’)
plt.plot([0,5],[8,8],’m’)
plt.plot([5,10],[8,8],’m’)

plt.axis(’off’)
plt.show()
Building Frame
Building Frame

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([0,0],[0,4],’b’)
plt.plot([0,0],[4,8],’b’)
plt.plot([5,5],[0,4],’r’)
plt.plot([5,5],[4,8],’r’)
plt.plot([10,10],[0,4],’b’)
plt.plot([10,10],[4,8],’b’)

plt.plot([0,5],[4,4],’k’)
plt.plot([5,10],[4,4],’k’)
plt.plot([0,5],[8,8],’m’)
Building Frame

ax.text(4,-1,’Plane Frame’)
ax.text(-0.5,0,’1’)
ax.text(-0.5,4,’4’)
ax.text(-0.5,8,’7’)
ax.text(5.1,0.25,’2’)
ax.text(5.1,4.25,’5’)
ax.text(5,8.25,’8’)
ax.text(10.25,0,’3’)
ax.text(10.25,4,’6’)
ax.text(10.25,8,’9’)

plt.axis(’off’)
Building Frame
Building Frame
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([0,0],[0,4],’b’)
plt.plot([0,0],[4,8],’b’)
plt.plot([5,5],[0,4],’r’)
plt.plot([5,5],[4,8],’r’)
plt.plot([10,10],[0,4],’b’)
plt.plot([10,10],[4,8],’b’)

plt.plot([0,5],[4,4],’k’)
plt.plot([5,10],[4,4],’k’)
plt.plot([0,5],[8,8],’m’)
plt.plot([5,10],[8,8],’m’)
Building Frame

ax.text(4,-1,’Plane Frame’)
ax.text(-0.5,0,’1’)
ax.text(-0.5,4,’4’)
ax.text(-0.5,8,’7’)
ax.text(5.1,0.25,’2’)
ax.text(5.1,4.25,’5’)
ax.text(5,8.25,’8’)
ax.text(10.25,0,’3’)
ax.text(10.25,4,’6’)
ax.text(10.25,8,’9’)
Building Frame

p1=mpatches.Circle((-0.2,2),0.2,color=’k’,fill=False)
ax.add_patch(p1)
ax.text(-0.3,1.9,’1’)
p2=mpatches.Circle((-0.2,6),0.2,color=’k’,fill=False)
ax.add_patch(p2)
ax.text(-0.3,5.9,’2’)

p3=mpatches.Circle((4.8,2),0.2,color=’k’,fill=False)
ax.add_patch(p3)
ax.text(4.7,1.9,’3’)
p4=mpatches.Circle((4.8,6),0.2,color=’k’,fill=False)
ax.add_patch(p4)
ax.text(4.7,5.9,’4’)
Building Frame
p5=mpatches.Circle((9.8,2),0.2,color=’k’,fill=False)
ax.add_patch(p5)
ax.text(9.7,1.9,’5’)
p6=mpatches.Circle((9.8,6),0.2,color=’k’,fill=False)
ax.add_patch(p6)
ax.text(9.7,5.9,’6’)

p7=mpatches.Circle((2.3,4.3),0.2,color=’k’,fill=False
)
ax.add_patch(p7)
ax.text(2.2,4.2,’7’)
p8=mpatches.Circle((7.3,4.3),0.2,color=’k’,fill=False
)
ax.add_patch(p8)
ax.text(7.2,4.2,’8’)
Building Frame

p9=mpatches.Circle((2.3,8.3),0.2,color=’k’,fill=False
)
ax.add_patch(p9)
ax.text(2.2,8.2,’9’)
p10=mpatches.Circle((7.35,8.3),0.25,color=’k’,fill=Fa
lse)
ax.add_patch(p10)
ax.text(7.2,8.2,’10’)

plt.axis(’off’)
plt.show()
Building Frame
Summary
Summary

Two-dimensional graphics using matplotlib is explained


References
References

Programming Python, Mark Lutz, O’Reilly,2012

You might also like