WikiGalaxy

Personalize

Using Pandas for Plotting Trends

Pandas, a powerful data manipulation library in Python, is widely used for data analysis and visualization. It provides easy-to-use data structures and data analysis tools that make it simple to plot trends in data. By leveraging Pandas' integration with Matplotlib, users can create a variety of plots to visualize data trends effectively.

Line Plot of Stock Prices

Line Plot of Stock Prices

Line plots are useful for visualizing trends over time. They display data points connected by straight lines, making it easy to see how values change over a period.


import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Date': ['2023-01-01', '2023-01-02', '2023-01-03'],
        'Stock_Price': [150, 152, 148]}
df = pd.DataFrame(data)

# Plotting
df['Date'] = pd.to_datetime(df['Date'])
df.plot(x='Date', y='Stock_Price', kind='line')
plt.title('Stock Price Over Time')
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()
    

Explanation

This example demonstrates how to create a line plot using Pandas. We first create a DataFrame with sample stock price data. By specifying the 'Date' column as the x-axis and 'Stock_Price' as the y-axis, we generate a line plot that shows the trend of stock prices over time.

Bar Plot of Sales Data

Bar Plot of Sales Data

Bar plots are ideal for comparing quantities corresponding to different groups. They use rectangular bars to represent data values.


import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Product': ['A', 'B', 'C'],
        'Sales': [200, 150, 250]}
df = pd.DataFrame(data)

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

Explanation

Here, we use a bar plot to visualize sales data for different products. Each bar represents a product's sales, allowing for easy comparison between them.

Histogram of Age Distribution

Histogram of Age Distribution

Histograms are used to represent the distribution of numerical data by dividing data into bins and plotting the frequency of data points in each bin.


import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Age': [23, 45, 31, 35, 40, 29, 22, 30, 38, 41]}
df = pd.DataFrame(data)

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

Explanation

The histogram displays the distribution of ages in our dataset. By specifying the number of bins, we can control the granularity of the distribution displayed.

Scatter Plot of Height vs Weight

Scatter Plot of Height vs Weight

Scatter plots are useful for visualizing the relationship between two numerical variables. Each point represents an observation in the dataset.


import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Height': [160, 170, 165, 155, 180],
        'Weight': [60, 70, 65, 55, 80]}
df = pd.DataFrame(data)

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

Explanation

This scatter plot illustrates the relationship between height and weight. It helps identify any correlation or pattern between these two variables.

Pie Chart of Market Share

Pie Chart of Market Share

Pie charts are used to represent the proportion of categories in a whole. Each slice of the pie represents a category's contribution to the total.


import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Company': ['A', 'B', 'C'],
        'Market_Share': [40, 35, 25]}
df = pd.DataFrame(data)

# Plotting
df.set_index('Company')['Market_Share'].plot(kind='pie', autopct='%1.1f%%')
plt.title('Market Share by Company')
plt.ylabel('')
plt.show()
    

Explanation

This pie chart shows the market share distribution among different companies. The autopct parameter is used to display the percentage of each slice.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025