WikiGalaxy

Personalize

Basic Data Visualization with Pandas

Introduction

Data visualization is an essential part of data analysis, providing insights into data patterns and trends. Pandas, a powerful data manipulation library in Python, offers built-in capabilities for data visualization, making it easy to create plots directly from DataFrames.

Line Plot

Understanding Line Plots

Line plots are useful for visualizing data trends over time. With Pandas, you can easily create line plots using the plot method on a DataFrame.


import pandas as pd
import matplotlib.pyplot as plt

data = {'Year': [2015, 2016, 2017, 2018, 2019],
        'Sales': [200, 220, 250, 270, 300]}
df = pd.DataFrame(data)

df.plot(x='Year', y='Sales', kind='line')
plt.title('Yearly Sales')
plt.xlabel('Year')
plt.ylabel('Sales')
plt.show()
        

Key Points

The above example demonstrates how to create a simple line plot to visualize sales data over the years. The plot method is versatile, allowing for customization of titles, labels, and other plot elements.

Bar Plot

Understanding Bar Plots

Bar plots are ideal for comparing quantities across different categories. Pandas allows you to create bar plots using the plot method with kind='bar'.


import pandas as pd
import matplotlib.pyplot as plt

data = {'Product': ['A', 'B', 'C', 'D'],
        'Sales': [300, 450, 150, 400]}
df = pd.DataFrame(data)

df.plot(x='Product', y='Sales', kind='bar')
plt.title('Product Sales Comparison')
plt.xlabel('Product')
plt.ylabel('Sales')
plt.show()
        

Key Points

This example illustrates how to use a bar plot to compare sales across different products. Bar plots are effective for visualizing categorical data and highlighting differences between groups.

Histogram

Understanding Histograms

Histograms are used to represent the distribution of numerical data. In Pandas, you can create histograms using the plot method with kind='hist'.


import pandas as pd
import matplotlib.pyplot as plt

data = {'Scores': [88, 92, 79, 93, 85, 91, 76, 84, 89, 95]}
df = pd.DataFrame(data)

df['Scores'].plot(kind='hist', bins=5)
plt.title('Score Distribution')
plt.xlabel('Scores')
plt.ylabel('Frequency')
plt.show()
        

Key Points

The histogram in this example shows the distribution of test scores. Histograms are useful for understanding the frequency distribution of a dataset and identifying patterns or outliers.

Scatter Plot

Understanding Scatter Plots

Scatter plots are used to visualize the relationship between two variables. Pandas allows you to create scatter plots using the plot method with kind='scatter'.


import pandas as pd
import matplotlib.pyplot as plt

data = {'Height': [150, 160, 170, 180, 190],
        'Weight': [50, 60, 70, 80, 90]}
df = pd.DataFrame(data)

df.plot(x='Height', y='Weight', kind='scatter')
plt.title('Height vs Weight')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.show()
        

Key Points

In this example, the scatter plot illustrates the relationship between height and weight. Scatter plots are effective for identifying correlations and patterns between two continuous variables.

Pie Chart

Understanding Pie Charts

Pie charts are used to represent the proportion of categories within a whole. Pandas provides the functionality to create pie charts using the plot method with kind='pie'.


import pandas as pd
import matplotlib.pyplot as plt

data = {'Category': ['A', 'B', 'C', 'D'],
        'Values': [40, 30, 20, 10]}
df = pd.DataFrame(data)

df.set_index('Category').plot(kind='pie', y='Values', autopct='%1.1f%%')
plt.title('Category Distribution')
plt.ylabel('')
plt.show()
        

Key Points

The pie chart in this example shows the distribution of values across different categories. Pie charts are useful for visualizing the relative proportions of parts of a whole.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025