Plots are used to convey different ideas. For example, using some plots, you may want to visualise the spread of data across two variables. With some others, you may want to gauge the frequency of a certain label. Depending on the objective of your visualisation task, you will choose an appropriate plot. As part of this session, you will learn to do the same.
Let’s get started with the first plot, namely, a Bar Graph.
You will be using the subpackage pyplot
to build plots and graphs throughout this session. To load the subpackage, you need to run the following command:
import matplotlib.pyplot as plt
To recap, Matplotlib allows you to use a simple and intuitive workflow to create plots. Here are the important Matplotlib commands that you learnt in the video:
plt.show()
: Explicit command required to display the plot objectplt.bar(x_component, y_component)
: Used to draw a bar graph
A bar graph is helpful when you have to visualise a numeric feature (fact) across multiple categories. In the example discussed above, you plotted the sales amount (numeric feature) under three different product categories. Using the plot, you could easily distinguish the performance of each category.
Let us now learn to add elements to our graph to make it more understandable.
You can use the following codes to add a title and labels to your graph:
plt.xlabel()
, plt.ylabel()
: Specify labels for the x and y axesplt.title()
: Add a title to the plot objectYou can also try to make the charts more appealing by using different attributes like font size, colour, etc. Adding labels and a title helps a person to interpret the graphs presented better and relays the required information to the viewer.
Let us now learn how to change the colour of the bars that represent our data.
Using the attributes of plt.bar()
, you can make the desired changes in the bars of the graph. Till now, we have learnt how to add a title and labels to the axes and change the colour of a bar. The element that remains to be learnt is the ticks or marks on the axes. Let’s learn about the features that matplotlib offers with regard to this.
You can use the following code to change the values and ticks on the x and y axes of a graph:
plt.yticks(tick_values, tick_labels)
You can specify the tick values and the label easily through a list or an array.
Having understood the concepts covered in this segment, you are now familiar with how to build a bar graph and add or modify the required elements within it. In the next segment, you will learn about the another visualisation, namely, a Scatterplot.