In Matplotlib, we used legends to indicate a different type of dataset. Suppose a naive person or a person which don’t know how to code, if he or she see the plot he will never understand which line indicate what sort of data. In such scenario’s legends come. Legends are the labels of a specific data set. It not only shows the label but also the type of line by which it is to identify the data set.
Let’s take the example from the previous section and apply the legends to differentiate the data set visually.
Now we know that the red line for the original dataset, blue for dataset*3 and green for dataset*5.
To add the legend and labels in a figure, we have to add the additional argument which is label and assign the string value to it. The label will not show in the figure until we will call the legend function. Plt.legend() will display the labels that we assigned in the plot function as an argument as can be seen in the following the code.
As we have seen, we can plot multiple datasets in the same window. But that data set was related to each other what if we want to plot two different data sets and we want to plot on the same window but not overlapping each other. In this scenario, we will use subplot. Subplot allows us to make multiple windows in the main root window. The data set will not overlap each other and will be outputted as individual data.
Following is the code of plotting a subplot:
What the code mentioned above does it will first import the Matplotlib module and then make a random data set.
After that, we have initiated the instance of the figure. The figure is the window in which we want to plot the subplots. After initiating the figure, we initialize the subplot. It is compulsory to give the 3-digit integer of which 1st two digits must be twenty-one. And then we plot the data 1. The same code is applied to data 2 and in last we show the graph. The plotted graph looks as follows:
Here we can see that two graphs are plotted separately in a subplot, the blue color indicates one and another one is indicated by the red color. There will be no overlapping of the graph which can confuse when the dataset is complex.
Now we have learned the basics so we can move toward some other types of graph that Matplotlib is supporting. Matplotlib supports many types of graphs we will see some of the essential graph types like a bar graph, stach bar graph, etc.
Unlike a line graph which shows the continuous results. The bar graph shows the height concerning other label data. Bar graph can be represented as a horizontal bar graph and a vertical bar graph. We will learn both the type of bar graph, but first, we will plot the vertical bar graph in python.
Following is the code of plotting vertical bar graph:
We first imported the Matplotlib library. In the result we gave the data of boys passed, boys failed, girls passed, and girls failed percentage within a class. The x variable contains the list ranging from 0 to length of data set so that each data can be placed accordingly. Then we initialize the plot and initialize the var graph in which x and result variable are provided as an argument. The x tick function takes two arguments the list of the range of data and the labels at each element of x. Then in last, we show the data. Following graph is given as an output:
Now to make the horizontal bar chart, we have to make the following changes:
In the above code, we call barh function instead of a bar which initializes the horizontal bar graph. Moreover, create the label on y-axis instead of the x-axis. The bar graph looks like the following image:
The pie chart is another famous chart family. In a pie chart, we distribute the data in a circular 360 degree. The advantage of using the pie chart is we can see even the minute value with respects to the other value. It can give a clear visualization of the data set. Following is the code of a pie chart:
First, we assign the labels and then provide the data within a list. Here we have three primary data the positive data the negative data and the neutral data. The data that is given in the list is the percentage which will equally be distributed in a pie chart of 360 degrees. Then we have assigned the color to each data point. The lovers are assigned a light green color, hater with red color and neutral are given color blue. Then we initialize the pie chart and give color and percentage data as an argument. To identify each data point we call the function legend that will show each data and what it is representing. The tight layout function makes the pie chart tightly packed, and there will be no distance between two data points. In last we show the pie chart which looks like this: