Skip to main content

Data Analysis and Visualization with Matplotlib and Seaborn | TOP 10 code snippets for practice

Data visualization is an essential aspect of data analysis. It enables us to better understand the underlying patterns, trends, and insights within a dataset. Two of the most popular Python libraries for data visualization are Matplotlib and Seaborn. Both libraries are highly powerful, and they can be used to create a wide variety of plots to help researchers, analysts, and data scientists present data visually.

In this article, we will discuss the basics of both libraries, followed by the top 10 most used code snippets for visualization. We'll also provide links to free resources and documentation to help you dive deeper into these libraries.

Matplotlib and Seaborn: A Quick Overview

Matplotlib

Matplotlib is a low-level plotting library in Python. It allows you to create static, animated, and interactive plots. It provides a lot of flexibility but may require more code to create complex plots compared to Seaborn.

Matplotlib is especially useful when you need full control over the visual elements of a plot, like adjusting the axis, colors, legends, titles, and more.

Official Documentation: Matplotlib Documentation

Seaborn

Seaborn is built on top of Matplotlib and is designed to make it easier to create visually appealing and informative statistical graphics. It comes with a variety of high-level functions to create complex plots with fewer lines of code.

Seaborn is particularly helpful for statistical visualizations, such as correlation plots, box plots, and heatmaps.

Official Documentation: Seaborn Documentation

Top 10 Most Used Code Snippets for Data Visualization

1. Basic Line Plot with Matplotlib

A line plot is one of the most common plots for showing trends over time.

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y)
plt.title('Basic Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()

2. Basic Scatter Plot with Matplotlib

A scatter plot is useful to visualize the relationship between two continuous variables.

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

plt.scatter(x, y)
plt.title('Basic Scatter Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()

3. Histogram with Matplotlib

Histograms are used to display the distribution of a dataset.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=30, edgecolor='black')
plt.title('Histogram')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()

4. Box Plot with Seaborn

A box plot visualizes the distribution of numerical data and highlights outliers.

import seaborn as sns
import matplotlib.pyplot as plt

# Example data
tips = sns.load_dataset("tips")

sns.boxplot(x='day', y='total_bill', data=tips)
plt.title('Box Plot')
plt.show()

5. Heatmap with Seaborn

A heatmap is a great way to visualize a correlation matrix or any other grid of data.

import seaborn as sns
import matplotlib.pyplot as plt

# Example data
flights = sns.load_dataset("flights")

pivot_flights = flights.pivot_table(index='month', columns='year', values='passengers')
sns.heatmap(pivot_flights, cmap="YlGnBu", annot=True, fmt="d")
plt.title('Heatmap')
plt.show()

6. Pair Plot with Seaborn

A pair plot displays pairwise relationships between several variables in a dataset.

import seaborn as sns

# Example data
iris = sns.load_dataset("iris")

sns.pairplot(iris, hue='species')
plt.title('Pair Plot')
plt.show()

7. Bar Plot with Seaborn

Bar plots are commonly used to visualize categorical data.

import seaborn as sns
import matplotlib.pyplot as plt

# Example data
tips = sns.load_dataset("tips")

sns.barplot(x="day", y="total_bill", data=tips)
plt.title('Bar Plot')
plt.show()

8. Violin Plot with Seaborn

A violin plot is a combination of a box plot and a kernel density plot, useful for comparing distributions.

import seaborn as sns
import matplotlib.pyplot as plt

# Example data
tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", data=tips)
plt.title('Violin Plot')
plt.show()

9. Pie Chart with Matplotlib

A pie chart is used to show proportions or percentages of a whole.

import matplotlib.pyplot as plt

# Example data
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title('Pie Chart')
plt.show()

10. Regplot (Regression Plot) with Seaborn

A regression plot shows the relationship between two variables and fits a regression line to the data.

import seaborn as sns
import matplotlib.pyplot as plt

# Example data
tips = sns.load_dataset("tips")

sns.regplot(x="total_bill", y="tip", data=tips)
plt.title('Regression Plot')
plt.show()

Additional Resources

For anyone looking to learn more about data visualization with Matplotlib and Seaborn, here are some great free resources and documentation:

1. Matplotlib Documentation

  • Link: Matplotlib Documentation
  • This resource provides a comprehensive guide to all the functionalities of Matplotlib, including detailed tutorials, examples, and an API reference.

2. Seaborn Documentation

  • Link: Seaborn Documentation
  • Seaborn's official website includes an easy-to-follow user guide, gallery, and API reference for creating high-level statistical visualizations.

3. Kaggle: Python Data Visualization

  • Link: Kaggle Data Visualization
  • Kaggle offers free courses that teach the fundamentals of data visualization using Python libraries like Matplotlib and Seaborn.

4. Matplotlib Tutorial on W3Schools

  • Link: Matplotlib Tutorial
  • This tutorial on W3Schools is beginner-friendly, covering the basics of Matplotlib and providing easy-to-follow examples.

5. Seaborn Tutorial on TutorialsPoint

  • Link: Seaborn Tutorial
  • A free, beginner-friendly guide to Seaborn, explaining its features and how to use it for creating beautiful statistical plots.

6. Python Data Science Handbook (by Jake VanderPlas)

  • Link: Python Data Science Handbook
  • This book (available online for free) includes excellent sections on data visualization using Matplotlib and Seaborn.

Conclusion

Matplotlib and Seaborn are two of the most powerful libraries for data visualization in Python. While Matplotlib provides flexibility for custom visualizations, Seaborn simplifies the creation of complex statistical plots. The code snippets provided in this article should give you a solid foundation to start exploring these libraries. Don't forget to check out the recommended resources for further learning and practice. Happy visualizing!

Comments

Popular posts from this blog

Using NLP for Text Analytics with HTML Links, Stop Words, and Sentiment Analysis in Python

  In the world of data science, text analytics plays a crucial role in deriving insights from large volumes of unstructured text data. Whether you're analyzing customer feedback, social media posts, or web articles, natural language processing (NLP) can help you extract meaningful information. One interesting challenge in text analysis involves handling HTML content, extracting meaningful text, and performing sentiment analysis based on predefined positive and negative word lists. In this blog post, we will dive into how to use Python and NLP techniques to analyze text data from HTML links, filter out stop words, and calculate various metrics such as positive/negative ratings, article length, and average sentence length. Prerequisites To follow along with the examples in this article, you need to have the following Python packages installed: requests (to fetch HTML content) beautifulsoup4 (for parsing HTML) nltk (for natural language processing tasks) re (for regular exp...

Building the Best Product Recommender System using Data Science

In today’s fast-paced digital world, creating personalized experiences for customers is essential. One of the most effective ways to achieve this is through a Product Recommender System . By using Data Science , we can build systems that not only predict what users may like but also optimize sales and engagement. Here's how we can leverage ETL from Oracle , SQL , Python , and deploy on AWS to create an advanced recommender system. Steps to Build the Best Product Recommender System: 1. ETL Process with Oracle SQL The foundation of any data-driven model starts with collecting clean and structured data. ETL (Extract, Transform, Load) processes from an Oracle Database help us extract relevant product, customer, and transaction data. SQL Query Example to Extract Data: SELECT product_id, customer_id, purchase_date, product_category, price FROM sales_data WHERE purchase_date BETWEEN '2023-01-01' AND '2023-12-31'; This query fetches historical sales data, includin...

Building and Deploying Large Language Models (LLMs) with AWS, LangChain, Llama, and Hugging Face

Large Language Models (LLMs) have revolutionized the AI and machine learning landscape by enabling applications ranging from chatbots and virtual assistants to code generation and content creation. These models, which are typically built on architectures like GPT, BERT, and others, have become integral in industries that rely on natural language understanding and generation. In this blog post, we’ll walk you through the steps involved in building and deploying a large language model using popular tools and frameworks such as AWS Generative AI, LangChain, Llama, and Hugging Face. What Are Large Language Models (LLMs)? LLMs are deep learning models designed to process and generate human language. Trained on vast amounts of text data, they have the ability to understand context, answer questions, translate languages, and perform other text-based tasks. Some key attributes of LLMs: Transformers : LLMs are generally based on transformer architecture, which allows the model to focus o...