DATA VISUALIZATION USING PYPLOT FOR DUMMIES BY a DUMMY

Anush Somasundaram
6 min readNov 11, 2020

In a nut shell, data visualisation is the representation of information and data in a graphical format to analyse trends, outliers and patterns in data.

Pyplot is a library in the matplotlib python package, it provides the features of Matlab in python. If you’re well versed, then you’re probably not going to find anything new or interesting in this article. This article contains only the very basic tools available in the pyplot library.

To work with the tools in the pyplot library, we have to import the library from the matplotlib package, and since it’s a long tedious command to be typing every time we have to call it, the general custom is to import it as plt. I also import the numpy package as it is inevitable not use it when working with matplotlib.

Before we get on with the pyplot library, I would like to discuss a couple of functions from the numpy library, the first function that will prove useful to us is the array() function, this will let us convert lists into arrays.

The second function is the dtype() function, this allows us to check what datatype a certain array is.

The third function is the arange() function, its similar to the range function but it outputs an array.

The function takes four arguments -> arange(start, stop, step, dtype )The fourth and final numpy.

The last function I’m going to tell you about is linspace(). The linspace() function helps add equally spaced values in-between two values.

The function takes three arguments -> linspace(start, stop, number of values to be generated)

We can also add a value to all the elements in the array at the same time by (I’m not quite sure how to put it in words) — →

Data visualisation is the representation of data in colourful and understandable formats, we can represent data in the form of scatter plots, line graphs, bar charts, tables, pie charts and many other forms. The pyplot library allows us to create these graphs with data and it further allows us to customise the features of the graph such as the colour, width and marker types.

Scatter plots using pyplot->

Scatter plots are useful for identifying patterns and trends in data.The scatter() function in pyplot allows us to create scatter plots.

We can customise the marker and marker colour by passing the arguments marker=“ ” and color=“Name of required colour” and the size by passing a numerical value.

— > matplotlib.pyplot.scatter( dataset1, dataset2, size in numerical value ,marker=“_”,color=“_” )

Their are multiple markers and colours to choose from,visit https://matplotlib.org/3.1.1/api/markers_api.html for the various available markers.

Line graphs using pyplot →

A line graph is a type of chart which displays information as a series of datapoint called markers.

The pyplot library offers a plot() function to create line graphs. It also lets us choose colours, width, and type of the lines in the graph by passing the arguments, color=“_”, linewidth=_ and linestyle=“_”.

There are four types of line types to choose from they are :-

1.Solid -> “solid” (default)

2. Dashed -> “ dashed”

3. Dash Dot -> “dashdot”

4. Dotted -> “dotted”

— > matplotlib.pyplot.plot( dataset1, dataset2, color=“-”, linewidth=_ ,linestyle=“_”)

Dataset1 is the data values you want on the x axis and dataset 2 is the values you want on the y axis.

The next graph is the sin and cos values graph ->

Numpy comes handy in a lot of scenarios.

We can also add markers to the points on the graph the line graph is going through,

by specifying the marker argument in the plot function, we can change the colour , size and type of markers by the arguments marker=“_”, markeredgecolor=“_”, markersize=_ .

— > matplotlib.pyplot(x values, y values, marker=“-”,markeredgecolor=“ ”, markersize=_ )

Axis labels and title ->

We can label the x and y axis using the xlabel() and ylabel() function in pyplot.

A title can be added to the graph with the title() function in pyplot.

— > matplotlib.pyplot.xlabel(”__”)

— > matplotlib.pyplot.ylabel(”__”)

— > matplotlib.pyplot.title(”__”)

Bar graphs using pyplot →

A bar graph is a graphical display of data using bars and heights. A bar chart can be drawn vertically or horizontally using bars of different heights and widths.

Bar charts are useful for representing quantity and various other data in a easily readable format.

The bar() function in the pyplot library allows us to create bar graphs.

We can change the colour, width of each of these bars individually using the width=[…] and colour=[…] arguments.

— > plt.bar(dataset1, dataset2, width=[…],color=[…])

We can also create multiple bar graphs for comparison of datasets.(Read the comments ).

Pie charts using pyplot →

The pie chart is a circular statistical graphic, which is divided into slices to illustrate a numerical proportion. Typically, a pie chart is used to show part of a whole and often a percentage share.

The pie() function from pyplot helps us create pie charts, we can also change the colour of the slices, size, add percentages, label slices and explode slices of the pie chart using the colors=[…], labels=[…] and autopct=“_” arguments.

The pie chart is a for plotting single data range values only, it will calculate the share of individual elements in a data set.

The default shape of a pie chart produced by the function is an oval, but we can make it a circle using the axis() function from the pyplot library.

— >matplotlib.pyplot.axis(“equal”).

— >matplotlib.pyplot.pie(dataset, labels=[..],colors=[], autopct=“..format string..” ,explode=[units] )

These tools come in handy when we want to represent data in various ways, Suppose we have to represent the top 10 most viewed songs on YouTube, we can create graphs of various types and represent the data. We can represent the data in the form of a bar graph , pie chart or even a line graph.

Hence we can represent the same data in multiple ways to help understand patterns, and piece together information for a clearer picture , all thanks to the pyplot library in python.

Link to github -> https://github.com/AnushSomasundaram/Compsci_with_python/

References -> Sumitha Aurora(Computer Science with Python), matplotlib.org

--

--

Anush Somasundaram

Looking for interesting software projects (ML/DL/NLP anything).