Introduction to Data Visualization in Python (2024)

Introduction to Data Visualization in Python (3)

Data visualization is the discipline of trying to understand data by placing it in a visual context so that patterns, trends and correlations that might not otherwise be detected can be exposed.

Python offers multiple great graphing libraries that come packed with lots of different features. No matter if you want to create interactive, live or highly customized plots python has an excellent library for you.

To get a little overview here are a few popular plotting libraries:

In this article, we will learn how to create basic plots using Matplotlib, Pandas visualization and Seaborn as well as how to use some specific features of each library. This article will focus on the syntax and not on interpreting the graphs, which I will cover in another blog post.

In further articles, I will go over interactive plotting tools like Plotly, which is built on D3 and can also be used with JavaScript.

Matplotlib is the most popular python plotting library. It is a low-level library with a Matlab like interface which offers lots of freedom at the cost of having to write more code.

To install Matplotlib pip and conda can be used.

pip install matplotlib
or
conda install matplotlib

Matplotlib is specifically good for creating basic graphs like line charts, bar charts, histograms and many more. It can be imported by typing:

import matplotlib.pyplot as plt

Scatter Plot

To create a scatter plot in Matplotlib we can use the scatter method. We will also create a figure and an axis using plt.subplots so we can give our plot a title and labels.

Introduction to Data Visualization in Python (4)

We can give the graph more meaning by coloring in each data-point by its class. This can be done by creating a dictionary which maps from class to color and then scattering each point on its own using a for-loop and passing the respective color.

Introduction to Data Visualization in Python (5)

Line Chart

In Matplotlib we can create a line chart by calling the plot method. We can also plot multiple columns in one graph, by looping through the columns we want and plotting each column on the same axis.

Histogram

In Matplotlib we can create a Histogram using the hist method. If we pass it categorical data like the points column from the wine-review dataset it will automatically calculate how often each class occurs.

Introduction to Data Visualization in Python (7)

Bar Chart

A bar chart can be created using the bar method. The bar-chart isn’t automatically calculating the frequency of a category so we are going to use pandas value_counts function to do this. The bar-chart is useful for categorical data that doesn’t have a lot of different categories (less than 30) because else it can get quite messy.

Introduction to Data Visualization in Python (8)

Pandas is an open source high-performance, easy-to-use library providing data structures, such as dataframes, and data analysis tools like the visualization tools we will use in this article.

Pandas Visualization makes it really easy to create plots out of a pandas dataframe and series. It also has a higher level API than Matplotlib and therefore we need less code for the same results.

Pandas can be installed using either pip or conda.

pip install pandas
or
conda install pandas

Scatter Plot

To create a scatter plot in Pandas we can call <dataset>.plot.scatter() and pass it two arguments, the name of the x-column as well as the name of the y-column. Optionally we can also pass it a title.

Introduction to Data Visualization in Python (9)

As you can see in the image it is automatically setting the x and y label to the column names.

Line Chart

To create a line-chart in Pandas we can call <dataframe>.plot.line(). Whilst in Matplotlib we needed to loop-through each column we wanted to plot, in Pandas we don’t need to do this because it automatically plots all available numeric columns (at least if we don’t specify a specific column/s).

Introduction to Data Visualization in Python (10)

If we have more than one feature Pandas automatically creates a legend for us, as can be seen in the image above.

Histogram

In Pandas, we can create a Histogram with the plot.hist method. There aren’t any required arguments but we can optionally pass some like the bin size.

Introduction to Data Visualization in Python (11)

It’s also really easy to create multiple histograms.

Introduction to Data Visualization in Python (12)

The subplots argument specifies that we want a separate plot for each feature and the layout specifies the number of plots per row and column.

Bar Chart

To plot a bar-chart we can use the plot.bar() method, but before we can call this we need to get our data. For this we will first count the occurrences using the value_count() method and then sort the occurrences from smallest to largest using the sort_index() method.

Introduction to Data Visualization in Python (13)

It’s also really simple to make a horizontal bar-chart using the plot.barh() method.

Introduction to Data Visualization in Python (14)

We can also plot other data then the number of occurrences.

Introduction to Data Visualization in Python (15)

In the example above we grouped the data by country and then took the mean of the wine prices, ordered it, and plotted the 5 countries with the highest average wine price.

Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for creating attractive graphs.

Seaborn has a lot to offer. You can create graphs in one line that would take you multiple tens of lines in Matplotlib. Its standard designs are awesome and it also has a nice interface for working with pandas dataframes.

It can be imported by typing:

import seaborn as sns

Scatter plot

We can use the .scatterplot method for creating a scatterplot, and just as in Pandas we need to pass it the column names of the x and y data, but now we also need to pass the data as an additional argument because we aren’t calling the function on the data directly as we did in Pandas.

Introduction to Data Visualization in Python (16)

We can also highlight the points by class using the hue argument, which is a lot easier than in Matplotlib.

Introduction to Data Visualization in Python (17)

Line chart

To create a line-chart the sns.lineplot method can be used. The only required argument is the data, which in our case are the four numeric columns from the Iris dataset. We could also use the sns.kdeplot method which rounds of the edges of the curves and therefore is cleaner if you have a lot of outliers in your dataset.

Introduction to Data Visualization in Python (18)

Histogram

To create a histogram in Seaborn we use the sns.distplot method. We need to pass it the column we want to plot and it will calculate the occurrences itself. We can also pass it the number of bins, and if we want to plot a gaussian kernel density estimate inside the graph.

Introduction to Data Visualization in Python (19)
Introduction to Data Visualization in Python (20)

Bar chart

In Seaborn a bar-chart can be created using the sns.countplot method and passing it the data.

Introduction to Data Visualization in Python (21)

Now that you have a basic understanding of the Matplotlib, Pandas Visualization and Seaborn syntax I want to show you a few other graph types that are useful for extracting insides.

For most of them, Seaborn is the go-to library because of its high-level interface that allows for the creation of beautiful graphs in just a few lines of code.

Box plots

A Box Plot is a graphical method of displaying the five-number summary. We can create box plots using seaborns sns.boxplot method and passing it the data as well as the x and y column name.

Introduction to Data Visualization in Python (22)

Box Plots, just like bar-charts are great for data with only a few categories but can get messy really quickly.

Heatmap

A Heatmap is a graphical representation of data where the individual values contained in a matrix are represented as colors. Heatmaps are perfect for exploring the correlation of features in a dataset.

To get the correlation of the features inside a dataset we can call <dataset>.corr(), which is a Pandas dataframe method. This will give us the correlation matrix.

We can now use either Matplotlib or Seaborn to create the heatmap.

Matplotlib:

Introduction to Data Visualization in Python (23)

To add annotations to the heatmap we need to add two for loops:

Introduction to Data Visualization in Python (24)

Seaborn makes it way easier to create a heatmap and add annotations:

Introduction to Data Visualization in Python (25)

Faceting

Faceting is the act of breaking data variables up across multiple subplots and combining those subplots into a single figure.

Faceting is really helpful if you want to quickly explore your dataset.

To use one kind of faceting in Seaborn we can use the FacetGrid. First of all, we need to define the FacetGrid and pass it our data as well as a row or column, which will be used to split the data. Then we need to call the map function on our FacetGrid object and define the plot type we want to use, as well as the column we want to graph.

Introduction to Data Visualization in Python (26)

You can make plots a lot bigger and more complicated than the example above. You can find a few examples here.

Pairplot

Lastly, I will show you Seaborns pairplot and Pandas scatter_matrix , which enable you to plot a grid of pairwise relationships in a dataset.

Introduction to Data Visualization in Python (27)
Introduction to Data Visualization in Python (28)

As you can see in the images above these techniques are always plotting two features with each other. The diagonal of the graph is filled with histograms and the other plots are scatter plots.

Data visualization is the discipline of trying to understand data by placing it in a visual context so that patterns, trends and correlations that might not otherwise be detected can be exposed.

Python offers multiple great graphing libraries that come packed with lots of different features. In this article, we looked at Matplotlib, Pandas visualization and Seaborn.

If you liked this article consider subscribing on my Youtube Channel and following me on social media.

The code covered in this article is available as a Github Repository.

If you have any questions, recommendations or critiques, I can be reached via Twitter or the comment section.

Introduction to Data Visualization in Python (2024)

FAQs

What is data Visualisation in Python? ›

The process of finding trends and correlations in our data by representing it pictorially is called Data Visualization. To perform data visualization in python, we can use various python data visualization modules such as Matplotlib, Seaborn, Plotly, etc.

What are the basics of data visualization? ›

What is data visualization? Data visualization is the representation of data through use of common graphics, such as charts, plots, infographics and even animations. These visual displays of information communicate complex data relationships and data-driven insights in a way that is easy to understand.

What are the modules in Python for data visualization? ›

Here we have listed the top 10 popular python libraries for data visualization.
  1. Matplotlib. Matplotlib is one of the best python data visualization libraries for generating powerful yet simple visualization. ...
  2. Plotly. ...
  3. Seaborn. ...
  4. GGplot. ...
  5. Altair. ...
  6. Bokeh. ...
  7. Pygal. ...
  8. Geoplotlib.
Jan 19, 2024

Why Python is popular for data visualization? ›

Python is a good choice for data visualization because its many libraries make it easy to create data visualizations.

What are the three types of data Visualisation? ›

The three most common categories of data visualization are graphs, charts, and maps. By choosing the right type of visualization for your data, you can reveal insights, tell a story, and guide decision-making.

Which is the best data visualization tool for Python and why? ›

Matplotlib is the backbone of Data Visualization Python that provides an open-source platform for representing intricate patterns in meaningful ways. Matplotlib offers a wide range of plot options, modification features, and various functions for users to produce all sorts of visualizations.

What are the 5 C's of data visualization? ›

Data for business can come from many sources and be stored in a variety of ways. However, there are five characteristics of data that will apply across all of your data: clean, consistent, conformed, current, and comprehensive. The five Cs of data apply to all forms of data, big or small.

What are the 7 stages of data visualization? ›

  • 1 6.
  • Step 1: Define a clear purpose.
  • Step 2: Know your audience.
  • Step 3: Keep visualizations simple.
  • Step 4: Choose the right visual.
  • Step 5: Make sure your visualizations are inclusive.
  • Step 6: Provide context.
  • Step 7: Make it actionable.

What are the 4 pillars of data visualization? ›

The foundation of data visualization is built upon four pillars: distribution, relationship, comparison, and composition.

What are most popular data visualization in Python? ›

The most popular Python data visualization library is Matplotlib. This is in part because it's been around for over 2 decades but also because it's reliable and can create all the interactive charts you need.

What is the most popular data visualization library in Python? ›

1. Matplotlib. Matplotlib is a data visualization library and 2-D plotting library of Python It was initially released in 2003 and it is the most popular and widely-used plotting library in the Python community.

How do you display data in Python? ›

You can display program data to the console in Python with print() . To display objects to the console, pass them as a comma-separated list of arguments to print() .

Why is Python better than Excel for data visualization? ›

Python code is reproducible and compatible, which makes it suitable for further manipulation by other contributors who are running independent projects. Unlike the VBA language used in Excel, data analysis using Python is cleaner and provides better version control.

What is the best Python visualization for graph? ›

Ggplot is one of the best data visualization packages in python with a 3k+ stars rating on Github, based on the ggplot2 implementation for the R programming language. Using a high-level API, Ggplot can build data visualizations like bar charts, pie charts, histograms, scatterplots, error charts, and so on.

What is the purpose of data Visualisation? ›

Data visualization helps to tell stories by curating data into a form easier to understand, highlighting the trends and outliers. A good visualization tells a story, removing the noise from data and highlighting useful information.

What is data Visualisation in pandas? ›

It is a powerful tool for understanding complex data and communicating insights to others. Data visualization can be used for a variety of purposes, such as identifying trends, patterns, and outliers, and exploring relationships between variables. Pandas is a popular open-source data analysis library for Python.

Is Python a Visualisation tool? ›

Python-based systems offer various graphing libraries that help the data analysts to create live, interactive and highly customized data graphs. It helps them to represent different data sets and their relations visually.

References

Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 5628

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.