Data Visualization in Python
Data Visualization in Python
Prepared by
Anand G
Assistant Professor,
ECE, Department,
Dr. Mahalingam College of Engineering and Technology,
Pollachi-642003
Email: anandg@drmcet.ac.in
Matplotlib
• Matplotlib is a Python 2D plotting library
which produces publication quality figures in a
variety of hardcopy formats and interactive
environments across platforms.
Line Graph
• A line chart or line graph is a type of chart which displays
information as a series of data points called ‘markers’
connected by straight line segments.
• Line graphs are usually used to find relationship between
two data sets on different axis; for instance X, Y.
>>>import matplotlib.pyplot as plt
>>>x=[2,4,6]
>>>y =[1,3,5]
>>>plt.plot(x, y)
>>>plt.show()
Contd…
• Plot a sine wave
>>>import matplotlib.pyplot as plt
>>>import numpy as np
>>>x = np.linspace(0, 10, 1000)
>>>plt.plot(x, np.sin(x))
>>>plt.xlabel(‘time’)
>>>plt.ylabel(‘amplitude’)
>>>plt.title(‘Sine Wave’)
>>>plt.show()
Plot variables
Marker Marker Linestyle Linestyle
Code Displayed Colour Colour Code Displayed
Code Displayed
+ Plus Sign
- Solid Line
. Dot R Red
o Circle -- Dashed Line
B Blue
* Star .. Dotted Line
G Green
P Pentagon
C Cyan Dash Dotted
S Square -.
Line
M Magneta
X X Character
D Diamond Y Yellow
No
H Hexagon K Black None Connecting
Lines
^ Triangle W White
Multiple Subplots
>>>import matplotlib.pyplot as plt
>>>import numpy as np
>>>x = np.linspace(0, 10, 1000)
>>>plt.subplot(121)
>>>plt.plot(x, np.sin(x))
>>>plt.show()
>>>plt.subplot(122)
>>>plt.plot(x, np.cos(x))
>>>plt.show()
Scatterplot
• A Scatterplot displays the value of 2 sets of data on 2
dimensions
>>>import matplotlib.pyplot as plt
>>>import numpy as np
>>>x = np.random.randn(1, 50)
>>>y = np.random.randn(1,50)
>>>plt.scatter(x1, y, color='red', s=30)
>>>plt.xlabel('X axis')
>>>plt.ylabel('Y axis')
>>>plt.title('Scatter Plot')
>>>plt.show()
Histogram
• A histogram shows the frequency on the vertical axis and
the horizontal axis is another dimension. Usually it has
bins, where every bin has a minimum and maximum
value.
>>>import numpy as np
>>>import matplotlib.mlab as mlab
>>>import matplotlib.pyplot as plt
>>>x = [21,22,23,4,5,6,77,8,9,10,31,32,33,
34,35,36,37,18,49,50,100]
>>>num_bins = 5
>>>n, bins, patches = plt.hist(x, num_bins,
facecolor='blue', alpha=0.5)
>>>plt.show()
Bar plot
>>>import matplotlib.pyplot as plt >>>import
numpy as np
>>>import matplotlib.pyplot as plt
>>>objects = ('Python', 'C++', 'Java', 'Perl', 'R', 'Lisp‘)
>>> y_pos = np.arange(len(objects))
>>>performance = [10,8,6,4,2,1]
>>>plt.bar(y_pos, performance, align='center',
alpha=0.5)
>>> plt.xticks(y_pos, objects) >>>plt.ylabel('Usage')
>>>plt.title('Programming language usage')
>>>plt.show()
Pie Charts
>>>import matplotlib.pyplot as plt
>>>labels = 'Python', 'C++', 'Ruby', 'Java'
>>>sizes = [215, 130, 245, 210]
>>> colors = ['gold', 'yellowgreen', 'lightcoral',
'lightskyblue']
>>>explode = (0.1, 0, 0, 0) # explode 1st slice
>>>plt.pie(sizes, explode=explode, labels=labels,
colors=colors, shadow=True, startangle=140)
>>>plt.axis('equal')
>>>plt.show()
Thank You