Back to Python
2026-02-146 min read

Pandas Plotting (Python Programming)

Learn Pandas Plotting (Python Programming) step by step with clear examples and exercises.

Title: Pandas Plotting (Python Programming)

Why This Matters

Understanding how to plot data using Pandas is crucial for data analysis and visualization tasks, making it an essential skill for every Python programmer. It allows you to create informative and engaging graphs that help in understanding the underlying patterns and trends in your data, which is vital for making informed decisions or presenting findings effectively. Visualizing data can also make complex datasets more accessible and easier to understand for both technical and non-technical audiences.

Prerequisites

Before diving into Pandas plotting, you should have a good grasp of the following:

  1. Basic Python syntax and concepts
  2. Intermediate-level understanding of data structures like lists and dictionaries
  3. Familiarity with the Pandas library for data manipulation in Python
  4. Knowledge of Matplotlib, which is the underlying plotting library used by Pandas
  5. Understanding of statistical measures such as mean, median, standard deviation, and correlation
  6. Familiarity with Seaborn (optional but recommended), a Python data visualization library built on top of Matplotlib, offering a higher-level interface for creating informative and attractive plots

Core Concept

Pandas provides a high-performance data analysis toolkit that includes various methods for plotting data. The most commonly used functions are plot(), hist(), scatter_matrix(), and Seaborn's lineplot(), barplot(), pairplot(), and heatmap().

Creating Simple Line Plots

Let's create a simple line plot using the built-in iris dataset:

import pandas as pd
import matplotlib.pyplot as plt

Load iris dataset

data = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")

Plot sepal length vs petal length

data.plot(x='sepal length (cm)', y='petal length (cm)', kind='line')

plt.show()


In the code above, we first import the necessary libraries and load the `iris` dataset. Then, we create a line plot of sepal length versus petal length using the `plot()` function.

### Creating Histograms

To create histograms for numerical columns, you can use the `hist()` method:

Create histogram for sepal width

data['sepal width (cm)'].hist(bins=15)

plt.show()


In this example, we create a histogram for the 'sepal width (cm)' column by accessing it as a series and calling the `hist()` method on it.

### Scatter Matrices

Scatter matrices can be used to visualize the relationships between multiple variables in your dataset:

Create scatter matrix for iris dataset

sns.pairplot(data, hue='species')

plt.show()


In this code, we use the seaborn library's `pairplot()` function to create a scatter matrix for the iris dataset, with each species color-coded for easy comparison.

### Seaborn Examples

Seaborn offers additional plotting functions that make it easier to create attractive and informative plots:

Load tips dataset

tips = sns.load_dataset('tips')

Create line plot for total bill vs tip percentage

sns.lineplot(x='total_bill', y='tip', data=tips)

plt.title('Total Bill vs Tip Percentage')

plt.show()

Create bar chart to show tip distribution by sex

sns.countplot(x='sex', hue='tip', data=tips, palette='Set3')

plt.title('Tip Distribution by Sex')

plt.show()


In this example, we load the tips dataset and create a line plot of total bill versus tip percentage using Seaborn's `lineplot()`. We also create a bar chart to show tip distribution by sex using Seaborn's `countplot()`, which provides a more attractive and informative visualization compared to Pandas' `value_counts()` method.

Worked Example

Let's work through an example where we load a CSV file containing sales data and plot various charts to analyze the trends:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Load sales data

data = pd.read_csv('sales.csv')

Check first few rows of the dataset

print(data.head())

Create line plot for total sales over time

sns.lineplot(x='year', y='total sales', data=data)

plt.title('Total Sales Over Time')

plt.show()

Create bar chart for product sales by category

sns.barplot(x='category', y='units sold', data=data, hue='month')

plt.title('Units Sold by Category and Month')

plt.show()

Create heatmap to show relationships between variables

corr = data.corr()

sns.heatmap(corr, annot=True, cmap='coolwarm')

plt.title('Correlation Matrix')

plt.show()


In this example, we load a sales dataset from a CSV file and perform various analyses using line plots, bar charts, and a heatmap to visualize the relationships between different variables.

Common Mistakes

  1. Forgetting to import necessary libraries: Always ensure you have imported both Pandas and Matplotlib (or Seaborn) before attempting any plotting operations.
  2. Not specifying the x and y parameters correctly: When using plot(), make sure to specify the correct column names for both the x-axis and y-axis data.
  3. Using incorrect plot types: Use the appropriate plot type for your data—line plots for continuous variables, bar charts for categorical variables, etc.
  4. Not setting a title or labels: Always set a title for your plots and ensure that both the x-axis and y-axis are labeled correctly.
  5. Ignoring error messages: If you encounter errors while plotting, carefully read and address them to avoid frustration and improve your understanding of the underlying issue.

Subheadings under Common Mistakes:

  • Incorrect Data Formatting
  • Missing Data Handling
  • Axis Scaling Issues
  • Misleading Color Choices

Practice Questions

  1. Create a scatter plot for the relationship between sepal length and petal width in the iris dataset using both Pandas and Seaborn.
  2. Load a CSV file containing student grades and create a bar chart to show the distribution of grades by subject using both Pandas and Seaborn.
  3. Use the scatter_matrix() function to visualize the relationships between all variables in the iris dataset.
  4. Create a line plot for monthly sales data, with each product category color-coded for easy comparison, using both Pandas and Seaborn.
  5. Load a CSV file containing stock prices and create a heatmap to show the correlation between different stocks using both Pandas and Seaborn.

FAQ

Q: How do I customize the appearance of my plots?

A: You can customize various aspects of your plots, such as colors, fonts, and gridlines, using Matplotlib's rcParams function or by directly modifying plot properties like linewidth, marker, and color. Seaborn also provides a variety of pre-defined themes to create visually appealing plots.

Q: What if I encounter an error while plotting?

A: If you encounter an error, carefully read the error message to understand the issue and take appropriate action, such as ensuring that your data is properly formatted or that you have imported the necessary libraries.

Q: Can I use Pandas for 3D plotting?

A: While Pandas does not support 3D plotting directly, you can use Matplotlib's 3D plotting capabilities in conjunction with Pandas data to create 3D plots. Seaborn does not currently offer 3D plotting functionality.

Q: How do I save my plots as images?

A: You can save your plots as various image formats (e.g., PNG, JPEG, SVG) using Matplotlib's savefig() function or Seaborn's saveplot() function.

Q: What is the difference between Pandas and Seaborn for plotting?

A: While both libraries are used for data visualization in Python, Seaborn provides a higher-level interface that builds upon Matplotlib to make it easier to create attractive and informative plots. Pandas has more limited plotting capabilities but is still useful for creating basic charts. Seaborn also offers pre-defined themes, making it simpler to create visually appealing plots.

Pandas Plotting (Python Programming) | Python | XQA Learn