0% found this document useful (0 votes)
4 views63 pages

Swarang Raut EDVA - Experiment - 2 - Data Visualization Using Mat Plot Lib

The document provides an overview of data visualization using the Matplotlib library in Python, highlighting its capabilities for creating various types of plots. It includes code examples for plotting functions, handling categorical variables, and customizing plots with titles, labels, and grid lines. The content is structured as a course by Prof Nilesh Deotale from St John College, aimed at teaching Python programming skills.

Uploaded by

devyanigawade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views63 pages

Swarang Raut EDVA - Experiment - 2 - Data Visualization Using Mat Plot Lib

The document provides an overview of data visualization using the Matplotlib library in Python, highlighting its capabilities for creating various types of plots. It includes code examples for plotting functions, handling categorical variables, and customizing plots with titles, labels, and grid lines. The content is structured as a course by Prof Nilesh Deotale from St John College, aimed at teaching Python programming skills.

Uploaded by

devyanigawade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Data visualization using matplotlib, Skill-Based Lab

Course [Python Programming] (CSL405) by Prof Nilesh


Deotale, Department of Computer Engineering, St
John College of Engineering and Management, Palghar

May 8, 2021

1 Data Visualization using matplotlib


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, and four graphical user interface toolkits.

[4]: df_meal =
pd.read_csv('meal_info.csv')
df_meal.head()

[4] : meal_id category cuisine


Unnamed: 3
0 NaN 1885.0 Beverages Thai
1 NaN 1993.0 Beverages Thai
2 NaN 2539.0 Beverages Thai
3 NaN 1248.0 Beverages Indian
4 NaN 2631.0 Beverages Indian

https://towardsdatascience.com/data-visualization-using-matplotlib-16f1aae5ce70

[5] : import
matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some
numbers') plt.show()

1
plot is a versatile function, and will take an arbitrary number of arguments. For
example, to plot x versus y, you can write:

[6] : plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

[6] : [<matplotlib.lines.Line2D at 0x1e1f91cdaf0>]

2
Plotting with categorical variables

[15]: names = ['group_a', 'group_b',


'group_c'] values = [1, 10, 100]

plt.figure(figsize=(9, 3))

plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names,
values) plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()

3
https://www.analyticsvidhya.com/blog/2020/02/beginner-guide-
matplotlib-data visualization-exploration-python/

2 NumPy - Matplotlib

[1]: import numpy as np


from matplotlib import pyplot as plt

x =
np.arange(1,11)
y=2*x+5
plt.title("Matplotlib
demo") plt.xlabel("x
axis caption")
plt.ylabel("y axis
caption") plt.plot(x,y)
plt.show()

4
An ndarray object x is created from np.arange() function as the values on the x axis.
The corre sponding values on the y axis are stored in another ndarray object y.
These values are plotted using plot() function of pyplot submodule of matplotlib
package.
The graphical representation is displayed by show() function.
Instead of the linear graph, the values can be displayed discretely by adding a
format string to the plot() function.
To display the circles representing points, instead of the line in the above example,
use “ob” as the format string in plot() function.

[8]: import numpy as np


from matplotlib import pyplot as plt

x=
np.arange(1,11)
y=2*x+5
plt.title("Matplotlib
demo") plt.xlabel("x
axis caption")
plt.ylabel("y axis
caption")
plt.plot(x,y,"oy")
plt.show()

5
Sine Wave Plot

[12]: import numpy as np


import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on a


sine curve x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.title("sine wave
form")

# Plot the points using matplotlib


plt.plot(x, y)
plt.show()

6
3 subplot()
The subplot() function allows you to plot different things in the same figure. In the
following script, sine and cosine values are plotted.
Example

[17]: import numpy as np


import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine


curves x = np.arange(0, 3 * np.pi, 0.1)
y_sin =
np.sin(x) y_cos
= np.cos(x)

# Set up a subplot grid that has width 2 and


height 1, # and set the first such subplot as
active. plt.subplot(2, 1, 1)

# Make the first plot


plt.plot(x, y_sin)
plt.title('Sine')

# Set the second subplot as active, and make the second plot.

7
plt.subplot(2, 1,
2) plt.plot(x,
y_cos)
plt.title('Cosine'
)

# Show the figure.


plt.show
()

https://www.tutorialspoint.com/numpy/numpy_matplotlib.htm

4 Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually
imported under the plt alias:

[3]: import matplotlib.pyplot as plt

Draw a line in a diagram from position (0,0) to position (6,250)

[1]: import matplotlib.pyplot


as plt import numpy as
np

xpoints = np.array([0,
6]) ypoints =
np.array([0, 250])

plt.plot(xpoints, ypoints)

8
plt.show()
5 Matplotlib Plotting
Draw a line in a diagram from position (1, 3) to position (8, 10):

[4]: import matplotlib.pyplot


as plt import numpy as
np

xpoints = np.array([1,
8]) ypoints =
np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

9
Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

[19]: import matplotlib.pyplot


as plt import numpy as
np

xpoints = np.array([1,
8]) ypoints =
np.array([3, 10])

plt.plot(xpoints, ypoints,
'oy') plt.show()

10
6 Matplotlib Markers
You can use the keyword argument marker to emphasize each point with a
specified marker:

[31] : import
matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', color = 'black')


plt.show()

11
7 Matplotlib Line
Linestyle
You can use the keyword argument linestyle, or shorter ls, to change the style of
the plotted line:

[7] : import
matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle =
'dotted') plt.show()

12
Line Color
You can use the keyword argument color or the shorter c to set the color of the line:

[8] : import
matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, color =
'r') plt.show()

13
Multiple Lines
You can plot as many lines as you like by simply adding more plt.plot() functions:

[32] : import
matplotlib.pyplot as plt
import numpy as np

y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])

plt.plot(y
1)
plt.plot(y
2)

plt.show()

14
Matplotlib Labels and Title
With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x-
and y-axis.

[11] : import numpy


as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330]) plt.plot(x,

y)

plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.show()

15
[12] : import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330]) plt.plot(x,

y)

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.show()

16
[35] : import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}

plt.title("Sports Watch Data", fontdict = font1,


loc='left') plt.xlabel("Average Pulse", fontdict =
font2) plt.ylabel("Calorie Burnage", fontdict = font2)

plt.plot(x, y)
plt.show()

17
[33] : import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data", loc =


'right') plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)
plt.show()

18
8 Matplotlib Adding Grid Lines
Add Grid Lines to a Plot
With Pyplot, you can use the grid() function to add grid lines to the plot.

[36] : import numpy as np


import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid()

plt.show()

19
[37] : import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid(axis =

'x') plt.show()

20
Set Line Properties for the Grid
You can also set the line properties of the grid, like this: grid(color = ‘color’,
linestyle = ‘linestyle’, linewidth = number).

[64]: import numpy


as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid(color = 'black', linestyle = '--', linewidth =

0.8) plt.show()

21
9 Matplotlib Subplots
Display Multiple Plots
With the subplots() function you can draw multiple plots in one figure:

[13] : import
matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2,
1) plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2,
2) plt.plot(x,y)

22
plt.show()
[21] : import
matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 1,
1) plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 1,
2) plt.plot(x,y)

plt.show()

23
[22] : import
matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3,
1) plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3,
2) plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3,
3) plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3, 4)

24
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3,
5) plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3,
6) plt.plot(x,y)

plt.show()

10 Matplotlib Scatter
With Pyplot, you can use the scatter() function to draw a scatter plot.
The scatter() function plots one dot for each observation. It needs two arrays of the
same length, one for the values of the x-axis, and one for values on the y-axis:

[44]: import matplotlib.pyplot


as plt import numpy as
np

25
x=
np.array([5,7,8,7,2,17,2,9,4,11,12,9,6
])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])

plt.scatter(x,
y) plt.grid()
plt.show()
Compare Plots

[62]: import matplotlib.pyplot


as plt import numpy as
np

#day one, the age and speed of 13 cars:


x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)

#day two, the age and speed of 15 cars:


x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)
plt.title("Graph of age vs. speed of cars",
loc='right') plt.grid()

plt.show()

26
How to Use the ColorMap

[49]: import matplotlib.pyplot


as plt import numpy as
np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.show()

27
[27]: import matplotlib.pyplot
as plt import numpy as
np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.colorba

r()

plt.show()

28
11 Creating Bars

[28]: import matplotlib.pyplot


as plt import numpy as
np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x,
y)
plt.show(
)

29
[50]: x = ["APPLES",
"BANANAS"] y = [400,
350]
plt.barh(x, y)

[50]: <BarContainer object of 2 artists>

30
[30]: import matplotlib.pyplot
as plt import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])
plt.barh(x, y)
plt.show()
[52]: import matplotlib.pyplot
as plt import numpy as
np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color = "orange")


plt.show()

31
[32]: import matplotlib.pyplot
as plt import numpy as
np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color =
"hotpink") plt.show()

32
[33]: import matplotlib.pyplot
as plt import numpy as
np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color =
"#4CAF50") plt.show()

33
[54]: import matplotlib.pyplot
as plt import numpy as
np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, width = 0.2)


plt.show()

34
12 Histograms

[35]: import numpy as np

x = np.random.normal(170, 10,

250) print(x)

[177.86871606 171.04178915 174.99842855 172.65375045 151.88593089


165.59179688 173.36164937 174.31706098 185.75086355
171.23477491 186.1270614 166.4204347 171.86517282 195.98830617
186.38726447 173.51303127 150.00306913 177.4381564 174.58999654
180.40915742 187.24080361 177.00463614 179.543501 183.24733095
176.98746693 156.90714551 173.08429676 198.34861733
169.98581647 166.05705751 187.0028892 188.39102213
176.37234649
174.54942031 155.46332601 184.01318939 193.14710003
152.74689845 174.58783322 169.69925064 166.78908993
147.04187308 195.53939653 169.79268269 167.350376
173.58239078 145.5895213 186.05676415 184.62522837 169.67500437
169.71497471 175.56058171 169.03851214 174.94005866
164.21456638 167.1838029 169.61649169 190.14059971
182.22672309
154.81821363 164.00783074 158.65721169 164.23470539
168.38935868 173.83119621 185.71556342 179.42704958
186.55458764 177.33005352 170.32118741 148.42926562
170.57295414 164.79146441 166.08633943 176.24230781
174.89131943 179.25201557 158.42298244 179.02099709
165.93761616 163.48984486 162.39292894 173.05522656
186.94608407 180.44692671

35
154.37753861 163.46567194 178.00807446 170.41912649
173.91867504 150.36630494 165.86516655 177.21772736
171.5671303 167.44261768 165.78522522 166.6951350
5 3
164.5426033 172.22219517 179.82579043 185.7161337
7 173.815575 4
176.6544952 167.17253785 177.91387738 196.2264890
9 9
163.9331979 165.83426282 153.14264381 167.3050449
4 4
180.48247912 158.56292053 185.12892604 177.09692945
181.5782867
161.0124517 147.74236133 172.53947262 176.0819981
2 5
187.3116221 183.82127321 170.04524094 182.5892489
3 4
165.6457393 168.86446769 182.68654769 168.3542930
3 8
186.1942676 175.44148428 173.24166144 156.2346612
8 9
164.5602262 176.31608018 157.43987254 158.2527170
1 7
174.5374359 168.60789116 163.72791913 164.8726108
8 8
175.3236361 178.81681385 158.58018488 171.8866398
3 5
175.57211651 177.50795711 173.05160098 155.17650259 155.68036194
175.95547294 183.4907567
169.28011318 182.7370372 174.03300917 171.23357982 172.68015405
159.69863777 174.77865085 163.77092132 177.20456837
175.95106648 170.68317233 188.24493033 169.85077928
170.30806585 174.90665702 172.73844951 164.70293911
178.68376852 171.94170695 181.7505992
178.27183175 172.85012378 179.70606277 169.86444569
164.45766702 162.81358317 167.90972944 168.29847279
153.59567305 172.10092897 194.70817638 171.37479412
153.65073425 192.70733483 177.95200187 194.59130729
164.12327666 156.34017766 168.82111662 165.85703871
171.69526936
182.77650728 167.86257122 170.05452116 162.52695186
176.46584841 158.48428112 146.79585038 175.87399185
154.44083826 175.21101111 182.53508318 183.54604283
178.71655255
175.1568672
161.50630632 158.44902915 175.98948595 159.37677198 152.94546174
169.29499708 184.25400769 149.31786908 166.59019776 189.2470742
176.89838158 154.16657696 172.4612624 167.87978116 168.79606594
190.66389446 160.24652162 165.82501326 166.71139354 182.43794604
174.94068706 162.7707296 170.06320469 161.58882441 174.43178957
149.91035598 172.80890435 177.27981039 163.38037754 172.24024051
177.13805581 150.90553508 167.88075212 185.89463132 173.92486184
170.29751486 185.88347213 161.63133081 170.59653835 163.75066153
181.3407372 177.29377598 145.1437543 184.35829796 173.60211275]

[56]: import matplotlib.pyplot


as plt import numpy as
np

x = np.random.normal(170, 10,

250) plt.hist(x)
plt.show()
36

13 Matplotlib Pie Charts

[37]: import matplotlib.pyplot


as plt import numpy as
np

y = np.array([35, 25, 25, 15])

plt.pie(y)
plt.show()
37

Labels
Add labels to the pie chart with the label parameter.
The label parameter must be an array with one label for each wedge:

[39]: import matplotlib.pyplot


as plt import numpy as
np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels =
mylabels) plt.show()
38

Start Angle
As mentioned the default start angle is at the x-axis, but you can change the start
angle by speci fying a startangle parameter.
The startangle parameter is defined with an angle in degrees, default angle is 0:

[40]: import matplotlib.pyplot


as plt import numpy as
np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels, startangle = 90)


plt.show()
39

Explode
Maybe you want one of the wedges to stand out? The explode parameter allows
you to do that.
The explode parameter, if specified, and not None, must be an array with one value
for each wedge.
Each value represents how far from the center each wedge is displayed:

[60]: import matplotlib.pyplot


as plt import numpy as
np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries",
"Dates"] myexplode = [0.2, 0, 0, 0]

plt.pie(y, labels = mylabels, explode =


myexplode) plt.show()
40

Shadow
Add a shadow to the pie chart by setting the shadows parameter to True:

[42] : import
matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries",
"Dates"] myexplode = [0.2, 0, 0, 0]

plt.pie(y, labels = mylabels, explode = myexplode, shadow =


True) plt.show()
41

Colors
You can set the color of each wedge with the colors parameter.
The colors parameter, if specified, must be an array with one value for each wedge:

[43] : import
matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries",
"Dates"] mycolors = ["black", "hotpink", "b",
"#4CAF50"]

plt.pie(y, labels = mylabels, colors =


mycolors) plt.show()
42

Legend
To add a list of explanation for each wedge, use the legend() function:

[44] : import
matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels =
mylabels) plt.legend()
plt.show()
43

Legend With Header


To add a header to the legend, add the title parameter to the legend function.

[45]: import matplotlib.pyplot


as plt import numpy as
np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)


plt.legend(title = "Four
Fruits:") plt.show()
44
https://www.w3schools.com/python/matplotlib_pie_charts.asp 45

You might also like