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

Introduction_of_matplotlib1

The document provides examples of various plotting techniques using Matplotlib in Python, including line plots, bar plots, pie charts, scatter plots, and histograms. It demonstrates how to visualize numerical and categorical data effectively, as well as how to create subplots for comparative analysis. Each example includes code snippets and descriptions of the plots' purposes and configurations.

Uploaded by

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

Introduction_of_matplotlib1

The document provides examples of various plotting techniques using Matplotlib in Python, including line plots, bar plots, pie charts, scatter plots, and histograms. It demonstrates how to visualize numerical and categorical data effectively, as well as how to create subplots for comparative analysis. Each example includes code snippets and descriptions of the plots' purposes and configurations.

Uploaded by

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

In [2]: import matplotlib.

pyplot as plt
import numpy as np

In [2]: #line plot


#line plot work on numerical data on both axis
cgpa = np.array([5, 6, 6.8, 7, 7.4, 8, 8.6, 10])
pacakge = np.array([5.5, 6.3, 7, 8, 7.5, 9, 10, 25])
font1 = {"family":"serif", "color":"blue", "size": 20}
font2 = {"family":"serif", "color":"brown", "size": 15}
plt.title("Shoolini unversity Btech students", fontdict= font1)
plt.xlabel("CGPA of students", fontdict = font2)
plt.ylabel("PACAKGE of students", fontdict = font2)
#ms is marker size, mec is marker edge color, mfc is marker face color
plt.plot(cgpa, pacakge, "h:r", ms = 20,mec = "orange", mfc = "green", linewidth = 5)
plt.grid(color = "green", linestyle = ":", linewidth = 1.5)
plt.show()

In [13]: #Bar plot vertical--it is work on numerical(number of fruit) and categorical(fruit name)
fruit = np.array(["Apple", "Banana", "Orange", "Watermelon"])
fruit_number = np.array([100, 80, 120, 50])
color = ["red", "yellow", "orange", "green"]

plt.bar(fruit, fruit_number, color = color)


plt.show()

In [4]: #horizontal bar


fruit = np.array(["Apple", "Banana", "Orange", "Watermelon"])
fruit_number = np.array([100, 80, 120, 50])
color = ["red", "yellow", "orange", "green"]

plt.barh(fruit, fruit_number, color = color)


plt.show()

In [5]: #pie plot--it is work on numerical(percentage of fruits) and categorical(fruit name) data
x = np.array([25, 35, 20, 20])
my_labels = ["Apple", "Banana", "Orange", "Watermelon"]
my_explode = [0, 0.2, 0, 0]

plt.pie(x, labels = my_labels, explode = my_explode)


plt.legend()
plt.show()

subplot used to users can easily create multiple plots in a single figure, making comparing and analyzing different datasets or variables
In [4]: import matplotlib.pyplot as plt
import numpy as np

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

plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is the first plot.
plt.plot(x,y)
plt.title("SALES")
plt.xlabel("Month")
plt.ylabel("Number of packet")

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

plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is the second plot.
plt.plot(x,y)
plt.title("INCOME")
plt.xlabel("Month")
plt.ylabel("Amount in thousands")

plt.suptitle("MY SHOP")
plt.show()

A scatter plot in Matplotlib is used to visually represent the relationship between two numerical variables by plotting individual data points on a graph
In [7]: #scatter plot
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])
plt.xlabel("Age of car")
plt.ylabel("Speed of car")
plt.title("Scatter plot of car")

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

Histogram A histogram is a graph showing frequency distributions. It is a graph showing the number of observations within each given interval.
In [8]: import numpy as np

x = np.random.randint(40, 100, (1, 200))


x = x.flatten()

print(x)

[73 92 61 71 58 42 60 86 90 61 82 50 68 72 53 90 67 82 71 65 90 48 57 85
58 71 66 48 93 50 44 51 86 75 84 86 43 95 46 40 46 46 51 76 60 42 91 41
41 79 56 53 55 41 79 87 75 40 94 92 72 67 49 43 99 68 90 80 89 86 73 58
66 69 91 42 80 53 60 72 49 87 44 72 92 41 41 76 71 56 89 96 72 51 91 65
40 76 50 83 45 45 77 88 74 53 93 50 97 88 55 55 66 96 43 85 79 70 71 93
77 92 95 66 99 59 79 93 45 69 66 68 41 42 70 42 41 89 43 50 54 45 68 64
92 74 56 52 73 63 56 61 50 61 92 85 52 88 84 88 87 57 68 66 76 47 96 87
40 83 70 45 56 72 42 51 42 43 51 89 42 57 84 59 85 83 70 68 62 89 52 49
98 85 79 57 73 63 53 72]

In [9]: plt.xlabel("Marks of student")


plt.ylabel("number of students")
plt.title("Histogram plot of students marks")
plt.hist(x)
plt.show()

In [ ]:

You might also like