Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔹 1. Introduction
Data visualization is an essential step in the data analysis
process. It allows you to explore the relationships between different variables
and communicate insights effectively. While Pandas provides some basic plotting
functionality, it is highly compatible with other visualization libraries like Matplotlib
and Seaborn.
In this chapter, we will cover:
By the end of this chapter, you’ll be able to visualize your
data easily and make your analyses more insightful and communicative.
🔹 2. Basic Plotting with
Pandas
Pandas has a simple plotting interface that integrates
seamlessly with Matplotlib, making it easy to create a variety of plots
with just a few lines of code.
✅ Plotting a Line Chart
The simplest plot you can create is a line chart. Here's an
example using Pandas' built-in plot() function:
import
pandas as pd
import
matplotlib.pyplot as plt
#
Sample data
data
= {'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
'Sales': [100, 120, 150, 170, 200,
220]}
df
= pd.DataFrame(data)
# Plotting the 'Sales' column
df.plot(x='Month',
y='Sales', kind='line', marker='o', title='Monthly Sales')
plt.show()
Output:
A simple line plot showing sales over the months.
✅ Plotting a Bar Chart
A bar chart is useful for comparing categorical data:
df.plot(x='Month',
y='Sales', kind='bar', color='skyblue', title='Monthly Sales')
plt.show()
Output:
A bar chart representing sales in each month.
🔹 3. Customizing Plots
Pandas' plot() function provides various parameters for
customizing the plot, such as colors, styles, and labels.
✅ Adding Titles and Labels
df.plot(x='Month',
y='Sales', kind='line', marker='o', title='Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
✅ Changing Line Style and Color
df.plot(x='Month',
y='Sales', kind='line', linestyle='--', color='green', marker='x',
title='Monthly Sales')
plt.show()
✅ Multiple Series in One Plot
You can plot multiple columns in a DataFrame on the same
plot:
df['Profit']
= [40, 60, 80, 100, 120, 140]
df.plot(x='Month',
y=['Sales', 'Profit'], kind='line', marker='o', title='Sales and Profit')
plt.show()
🔹 4. Advanced
Visualizations with Matplotlib
While Pandas provides basic plotting, Matplotlib
offers much more flexibility and advanced options.
✅ Creating a Scatter Plot
df.plot(kind='scatter',
x='Sales', y='Profit', color='red', title='Sales vs Profit')
plt.show()
✅ Creating a Histogram
Histograms are useful for understanding the distribution of
data:
df['Sales'].plot(kind='hist',
bins=10, color='lightblue', title='Sales Distribution')
plt.show()
✅ Customizing Plots with
Matplotlib
fig,
ax = plt.subplots(figsize=(8, 6))
ax.plot(df['Month'],
df['Sales'], label='Sales', color='blue', linestyle='-', marker='o')
ax.set_xlabel('Month')
ax.set_ylabel('Sales')
ax.set_title('Sales
Trend')
ax.legend()
plt.show()
🔹 5. Visualizing with
Seaborn
Seaborn is built on top of Matplotlib and provides a
high-level interface for creating attractive, informative statistical graphics.
It integrates well with Pandas.
✅ Scatter Plot with Seaborn
import
seaborn as sns
sns.scatterplot(x='Sales',
y='Profit', data=df, color='blue', title='Sales vs Profit')
plt.show()
✅ Box Plot with Seaborn
A box plot shows the distribution of data based on a
five-number summary:
sns.boxplot(x='Month',
y='Sales', data=df, palette='Set2')
plt.title('Sales
Distribution by Month')
plt.show()
🔹 6. Plotly Integration
for Interactive Visualizations
While Matplotlib and Seaborn are great for static plots, Plotly
provides interactive charts that are useful for exploratory analysis or sharing
data insights.
✅ Scatter Plot with Plotly
import
plotly.express as px
fig
= px.scatter(df, x='Sales', y='Profit', title='Sales vs Profit')
fig.show()
Plotly’s interactive charts allow for zooming, panning, and
hovering over data points to display additional information.
✅ Line Chart with Plotly
fig
= px.line(df, x='Month', y='Sales', title='Monthly Sales')
fig.show()
🔹 7. Summary Table
Plot Type |
Function/Method |
Description |
Line Chart |
df.plot(kind='line') |
Plot data
over a continuous range (x-axis) |
Bar Chart |
df.plot(kind='bar') |
Compare
quantities across different categories |
Scatter Plot |
df.plot(kind='scatter') |
Visualize the
relationship between two variables |
Histogram |
df['column'].plot(kind='hist') |
Plot the
distribution of a single variable |
Box Plot |
sns.boxplot() |
Show the
distribution of data with quartiles |
Custom Plot |
plt.subplots() |
Fine-tune
figure size, axes, and labels |
Plotly
Interactive Plot |
px.line() or
px.scatter() |
Create
interactive plots with zoom and hover |
Pandas is a Python library for data manipulation and analysis, providing powerful data structures like DataFrames and Series.
While NumPy is great for numerical operations, Pandas is designed for working with structured data, including heterogeneous data types (strings, dates, integers, etc.) in a tabular format
A DataFrame is a two-dimensional data structure in Pandas, similar to a table or spreadsheet, with rows and columns. It’s the core structure for working with data in Pandas.
A Series is a one-dimensional data structure that can hold any data type (integers, strings, etc.), similar to a single column in a DataFrame.
You can load data using functions like pd.read_csv() for CSV files, pd.read_excel() for Excel files, and pd.read_sql() for SQL databases.
Yes — Pandas provides functions like fillna() to fill missing values, dropna() to remove rows/columns with missing data, and isna() to identify missing values.
You can filter data using conditions. For example: df[df['Age'] > 30] filters rows where the 'Age' column is greater than 30.
✅ Yes — use the groupby() function to group data by one or more columns and perform aggregations like mean(), sum(), or count().
Pandas integrates well with Matplotlib and provides a plot() function to create basic visualizations like line charts, bar charts, and histograms
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)