Swarang Raut EDVA - Experiment - 2 - Data Visualization Using Mat Plot Lib
Swarang Raut EDVA - Experiment - 2 - Data Visualization Using Mat Plot Lib
May 8, 2021
[4]: df_meal =
pd.read_csv('meal_info.csv')
df_meal.head()
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:
2
Plotting with categorical variables
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
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.
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
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
# 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'
)
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:
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):
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):
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
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
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
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.
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.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.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.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.
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.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.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).
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)
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:
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
plt.show()
26
How to Use the ColorMap
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.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.colorba
r()
plt.show()
28
11 Creating Bars
plt.bar(x,
y)
plt.show(
)
29
[50]: x = ["APPLES",
"BANANAS"] y = [400,
350]
plt.barh(x, y)
30
[30]: import matplotlib.pyplot
as plt import numpy as np
31
[32]: import matplotlib.pyplot
as plt import numpy as
np
plt.bar(x, y, color =
"hotpink") plt.show()
32
[33]: import matplotlib.pyplot
as plt import numpy as
np
plt.bar(x, y, color =
"#4CAF50") plt.show()
33
[54]: import matplotlib.pyplot
as plt import numpy as
np
34
12 Histograms
x = np.random.normal(170, 10,
250) print(x)
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]
x = np.random.normal(170, 10,
250) plt.hist(x)
plt.show()
36
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:
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:
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:
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
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
Legend
To add a list of explanation for each wedge, use the legend() function:
[44] : import
matplotlib.pyplot as plt
import numpy as np
plt.pie(y, labels =
mylabels) plt.legend()
plt.show()
43