Matplotlib is a widely used visualization library in Python that allows easy creation of various types of line charts, bar charts, histograms, etc. Since Matplotlib is a visualization library, each example can be quite extensive. Here, we will try to understand this library well with simple and minimal code.
Installing Matplotlib
Similar to installing other packages, we will install Matplotlib using the pip command. Since we have already learned how to use the pip command, we won’t go into details here. To install Matplotlib, run the following command:
pip install matplotlib
If everything goes well, you should see something like the image below:
I hope everyone has successfully installed Matplotlib.
Plotting
In Matplotlib, graphs can be easily drawn using the plot()
method. Here, we will show how to create a graph with only y-axis values.
import matplotlib.pyplot as plt
import numpy as np
y_value = np.array([3,6,4,1,2])
plt.plot(y_value, marker='*', linestyle='dashed')
plt.title("This is a title")
plt.xlabel("X Value")
plt.ylabel("Y Value")
plt.show()
Output:
Here we can see a graph. If we look closely, we will see that points are created at positions (0,3), (1,6), (2,4), (3,1), (4,2). Even though the y_value
variable only has the y-axis values, Matplotlib automatically takes the x-axis values one by one.
Notice in the first line, instead of importing matplotlib
, we imported matplotlib.pyplot
. Pyplot is a sub-module where most of the graph functions are defined.
Points are plotted using plt.plot()
. Here, we see an argument named marker
, which indicates the design of the points. Instead of *
, you can use any Python-supported character. Another argument here is linestyle
, which specifies the style of the line. Its values can be solid
, dotted
, dashed
, dashdot
, etc. If the linestyle
argument is not given, it defaults to solid
.
The plt.title()
function sets the title of the graph. The text “This is a title” seen above the graph is the title.
plt.xlabel()
and plt.ylabel()
are used to set the labels for the x-axis and y-axis, respectively. Compare the graph with the result provided.
The plt.show()
line is used to display the result.
Note: In subsequent examples, we will not use labels or titles.
Line Graph
The graph we saw above is also a line graph. Now let’s see how to create a graph with multiple points.
import matplotlib.pyplot as plt
import numpy as np
x_value = np.array([1,2,5,8])
y_value = np.array([3,6,9,5])
plt.plot(x_value,y_value)
plt.show()
Output:
Here, we can see a line graph with specific points.
Scatter Plot
In a line plot, one point is connected to another, but in a scatter plot, there are no connections. It looks like random points. The scatter()
method is used for scatter plots.
import matplotlib.pyplot as plt
import numpy as np
x_value = np.random.rand(20) #Genrate 20 random number
y_value = np.random.rand(20) #Genrate 20 random number
plt.scatter(x_value,y_value)
plt.show()
Output:
We have drawn a graph using scatter()
. For convenience, we used some random numbers in lines 4 and 5. You can try using some real numbers of your choice.
Bar Chart
Bar charts are used to represent data in bar form. The bar()
method is used to create a bar chart.
import matplotlib.pyplot as plt
import numpy as np
x_value = np.array(['CSE10','CSE20','CSE30'])
y_value = np.array([4,3.55,2.88])
plt.bar(x_value,y_value)
plt.show()
Output:
A plot has been created using the bar()
method. Bars are vertical by default, but you can create horizontal bars using the barh()
method.
Pie Chart
We are all somewhat familiar with pie charts. Pie charts are used to represent data as percentages. The pie()
method is used to create a pie chart in Matplotlib.
import matplotlib.pyplot as plt
import numpy as np
value = [20,20,30,10,20]
label = ['Label1','Label2','Label3','Label4','Label5']
plt.pie(value,labels=label)
plt.show()
Output:
A pie chart has been created using the pie()
method. Here, we have created some meaningless labels; in real work, we will try to create meaningful labels.