GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Machine Learning
  • Android
  • Java
  • .Net Core
  • PHP
  • Top Languages Poll
  • Kotlin
  • .Net
  • C#
  • Python
  • JavaScript
  • Data Visualization
  • Line Chart
  • CodingInfinite Staff

This view

  • Original page
  • New address

GeezerVison is showing codinginfinite.com — Data Visualization in Python using Simple Line Chart - MATPLOTLIB

In this article, we are going to use Python to visualize the data in a Simple Line Chart. Nowadays, the internet is being bombarded with a huge amount of data each second. According to the Sixth edition of Domo Inc. reports, over 2.5 quintillion bytes of data are generated each second. We can use the data of our interest to get insights about it and Data Visualization provides a way to see the data graphically which provides an easier way to see the trends, patterns, outliers etc. As a human, we understand easily the abstract things rather than getting into the details. So, data visualization allows the draw insights from data in an effective manner.

A visually appealing representation of data conveys more meaning to the user rather than in tabular or textual form.  It allows the user to see patterns and trends in the data that they never knew before.

You don’t need a heavy machine to visualize the complex data. You can quickly explore data using Python’s efficient libraries available. Python is highly used for data-intensive tasks. It can be genetics data, economic analysis, social media trend analysis, and much more.

Introducing matplotlib

matplotlib is one of the most popular mathematical plotting library available in Python. It is extensively used. We will use it to create different visualizations of data such as simple plots, line graphs, and scatter plots.

Pre-requisites

In order to follow this article series, we assume the following:

  • You have some experience in Python.
  • You have installed Python’s version 3.X
  • You have pip installed

Installing matplotlib

In order to install matplotlib and it dependencies for macOS, Windows and Linux distributions, open the command prompt/shell and type the following command:

Make sure you have latest pip version. In order to update pip you can use the following command

> python –m pip install –-upgrade  pip

> python –m pip install –U matplotlib

Testing matplotlib

In order to check whether matplotlib have successfully installed, open command prompt. Type following:

> python

>>> import matplotlib

If you don’t see any errors it means that matplotlib is successfully installed on your system. Now you can move to the next section of the article.

Types of Visualizations  in matplotlib

There are numerous visualizations available in matplotlib. You can see the full list at here. Let us enlist some of them:

  • Lines, bars and markers
    • Stacked Bar Graph
    • Horizontal Bar Chart
    • Grouped Bar Chart
    • and many more
  • Subplots, axes and figures
    • Aligning Labels
    • Geographic Projections
    • Multiple Subplots
    • and many more
  • Pie and Polar Charts
  • Statistics related graphs and charts

Plotting a Simple Line Graph

A graph with points connected by lines is called a line graph. Let’s plot a simple line graph using matplotlib, and then modify it according to our needs to create a more informative visualization of our data.

We will use a function named generate_square_series(n) which will generate square number sequence as data for the graph.

Create a file named mpl_squares_plotting.py in your favorite editor.

def generate_square_series(n):
    squares = []

    # i starts from 1 and ends at n-1

    for i in range(1, n):
        squares.append(i * i)
    return squares

Now that we have written a function to generate data. Let’s use matplotlib to visualize it.

import matplotlib.pyplot as plt
# Using the function we’ve created
squares = generate_square_series(100)
plt.plot(squares)
plt.show()

Explanation of Code

We first imported the pyplot module as plt so we don’t have to type pyplot repeatedly.

pyplot contains functions that help to generate charts and plots.

Then we used the function generate_square_series that returns a list and we have assigned it to squares object reference.

We then passed the squares list to the function plot() which plots the number.

plt.show() opens matplotlib’s viewer and displays the plot as shown in the figure below.

Simple Line Graph in matplotlib

Here’re more Articles, you might be interested:

— Top 5 Python Web Frameworks to Learn

— Here Are The Ten Best Programming Languages to learn

CodingInfinite Staff

Similar Posts

Python SQLite Tutorial – Creating Simple Query with Code Example

In this article, we will start learning to work with a Database using Python. Every useful application either, it is a desktop application, mobile application, or web application, uses some sort of database for the storage and retrieval of the data. In our previous article, we saw that we can achieve data persistence using files…

Database Constraints in SQLite using Python Code by Querying

This article is the continuation of the previous article in which we have seen how to create and query a simple database. In this article, we will learn the following: Database Constraints in SQLite (PRIMARY KEY, NOT NULL, UNIQUE, FOREIGN KEY) Querying Database We will follow the following relational schema to create the database. Create…

Make A Command-line based Image Re-sizer in Python 3 using Pillow

Python is a general-purpose programming language. It is so popular in software developers and has a vast number of plugins available to play with. It has a huge community to support new learners. In this article, we will see one of its plugin called “Pillow”. Pillow is an Imaging Library used o work with images,…

Best Python IDE and Code Editors | Top 10

As a developer, we always like to choose the best thing among several choices available, especially in the case of Integrated Development Environments (IDEs). Usually, developers and programmers get confused in choosing the best and most suitable IDE or Editor for them. In this article, we will go through some of the best IDEs available…

Creating GUI Application in Python – TKINTER Tutorial

Graphical User Interface A GUI (pronounced like “gooey”) allows the user to interact with the operating system using graphical elements such as icons, buttons, dialog boxes, inputs, etc. The interactions with GUI are mostly done with the help of small windows called dialog boxes. One part of the user interface consists of hardware devices such…

Web Login Page Tutorial using Django Authentication System | Python

In a medium or large web application, it is very common and required to have a user authentication system to deal with users management and offer resources based on identity. Web Developers can implement authentication system by their own but here two points must be considered, first, implementing a secure authentication system is challenging and…

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.