Data Visualization is the first step in data analysis. Data visualization with python is very simple. That’s why people choose python for data visualization. There are other languages for data visualization like R, Matlab, and Scala. Let’s First see what is data visualization.
What Is Data Visualization?
Data Visualization: Data visualization can be defined as the graphical representation of data or information by using graphs, charts, or maps.
To communicate information clearly and efficiently, data visualization uses statistical graphics, plots, information graphics, and other tools. Numerical data may be encoded using dots, lines, or bars, to visually communicate a quantitative message.
Why Data Visualization With Python?
As you all know python has a lot of great graphing libraries(Open Source) with great features. With these libraries you can create interactive, live, or highly customized plots or graphs.
There are some best python libraries for data visualization. like,
- Pandas Visualization
In this article, I’ll show you how to visualize data. For data visualization with python, we’ll use Matplotlib Library. That is a very powerful library for creating graphs and plots.
You can read documentation of matplotlib from HERE
To install matplotlib python library, First, open a terminal or command prompt and type this following command
pip install matplotlib
It will take some time to install matplotlib(Make sure you’re connected to Internet)
We also need two more library called NumPy and pandas. Let’s install it too. Open terminal and type:
pip install numpy
pip install pandas
Simple Data Visualization With Matplotlib
Let’s plot simple line graph using python. Open IDE or any text editor which you are using.
Related Post : 7 Best IDEs And Text Editors For Python
First, we have to import matplotlib library to use it in our code. So, let’s import. Type this code in your IDE:
import matplotlib.pyplot as plt
Now run this code if it give any error than maybe you don’t installed matplotlib library yet.
Now it’s time to visualize some data with python. Here is the code and it’s output. Don’t worry i’ll explain it.
#Importing Modules import matplotlib.pyplot as plt x = [1,5,10] y = [1,5,15] plt.plot(x,y) plt.show()
- First, we imported Matplotlib Library
- Then assign x =[1,5,10] and y = [1,5,15]
- We plotted a graph of x and y
That is the very simple data visualization with python. Now Let’s try a with NumPy.
#Importing Modules import matplotlib.pyplot as plt import numpy as np x = np.array([2,4,6,8,10]) y = x**2 plt.plot(y) plt.show()
- First, we imported two libraries Matplotlib and NumPy
- Then we created an array X and Y will give a square of that array (Note: List will not work that’s why we use an array here)
- We plot a graph and then show it.
This is the simple data visualization with python. Now let’s try to visualize data from a CSV file.
Data Visualization From CSV File
We can also load datasets with the pandas module. Here we used the dataset of the US Population from 2000 to 2021. You can easily download a dataset from here:
#Importing Library import matplotlib.pyplot as plt import numpy as np import pandas as pd #Load CSV File Using Pandas dataset = pd.read_csv('US_Population.csv') plt.plot(dataset.Year,dataset.Population) plt.title("US Population") plt.xlabel("Year") plt.ylabel("Population") plt.show()
- As we did before we imported 3 Libraries Matplotlib, NumPy and Pandas
- With pd.read_csv(), we load CSV File(You can download fro above link)
- dataset.Year is referred to as the column with the name “Year” and dataset.Population is referred to as the column with the name “Population”.
- You can check by printing it. Like print(dataset.Year)
- Then we add Graph title, give a name to x-axis and y-axis.
This is the another way of data visualization with python from CSV File.
Related Post: Plotting Line Graphs In Python Using Matplotlib