WikiGalaxy

Personalize

Using Matplotlib with Pandas

Overview:

Matplotlib is a powerful plotting library in Python that works seamlessly with Pandas, a data manipulation library. By integrating these two libraries, you can easily create a variety of plots directly from Pandas DataFrames and Series. This integration simplifies the process of visualizing data, making it more intuitive and efficient.

Line Plot

Line Plot with Pandas:

Line plots are useful for visualizing trends over time. With Pandas, you can create line plots directly from DataFrame columns.


import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Month': ['Jan', 'Feb', 'Mar', 'Apr'],
        'Sales': [200, 220, 250, 270]}
df = pd.DataFrame(data)

# Plotting
df.plot(x='Month', y='Sales', kind='line')
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
        

Explanation:

The code uses Pandas to create a DataFrame and Matplotlib to plot a line graph, showing sales trends over months.

Bar Chart

Bar Chart with Pandas:

Bar charts are effective for comparing quantities across categories. They can be generated from Pandas DataFrames using the 'bar' kind.


import pandas as pd
import matplotlib.pyplot as plt

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

# Plotting
df.plot(x='Product', y='Revenue', kind='bar')
plt.title('Product Revenue')
plt.xlabel('Product')
plt.ylabel('Revenue')
plt.show()
        

Explanation:

This example demonstrates how to use Pandas and Matplotlib to create a bar chart, which helps in comparing revenue across different products.

Histogram

Histogram with Pandas:

Histograms are useful for understanding the distribution of numerical data. Pandas makes it easy to generate histograms from DataFrame columns.


import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Age': [23, 45, 31, 35, 25, 43, 30]}
df = pd.DataFrame(data)

# Plotting
df['Age'].plot(kind='hist', bins=5)
plt.title('Age Distribution')
plt.xlabel('Age')
plt.show()
        

Explanation:

The histogram shows the distribution of ages in the dataset, helping to visualize the frequency of age ranges.

Scatter Plot

Scatter Plot with Pandas:

Scatter plots are ideal for identifying relationships between two variables. Pandas allows for easy creation of scatter plots from DataFrame data.


import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Height': [5.1, 5.5, 5.9, 6.0, 5.7],
        'Weight': [150, 160, 180, 190, 170]}
df = pd.DataFrame(data)

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

Explanation:

The scatter plot illustrates the relationship between height and weight, helping to identify any correlation between the two variables.

Pie Chart

Pie Chart with Pandas:

Pie charts are useful for showing proportions of a whole. Pandas can create pie charts from DataFrame data to represent categorical data distribution.


import pandas as pd
import matplotlib.pyplot as plt

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

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

Explanation:

The pie chart represents the distribution of values across different categories, illustrating their proportions as 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