Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Plot the dataset to display Uptrend – Python Pandas
An uptrend in time series analysis shows data values increasing over time. We can visualize uptrends using Pandas and Matplotlib to plot datasets against time. Let's demonstrate this with a sales dataset example.
Sample Data
First, let's create a sample sales dataset that demonstrates an uptrend pattern ?
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime, timedelta
# Create sample sales data with uptrend
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='M')
sales = [100 + i*10 + np.random.randint(-20, 20) for i in range(len(dates))]
# Create DataFrame
sales_data = pd.DataFrame({
'Date_of_Purchase': dates,
'Sales': sales
})
print("Sample Sales Data:")
print(sales_data.head())
Sample Sales Data: Date_of_Purchase Sales 0 2023-01-31 95 1 2023-02-28 118 2 2023-03-31 110 3 2023-04-30 150 4 2023-05-31 131
Converting Date Column
Convert the date column to datetime format for proper time series plotting ?
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime, timedelta
# Create sample data
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='M')
sales = [100 + i*10 + np.random.randint(-20, 20) for i in range(len(dates))]
sales_data = pd.DataFrame({
'Date_of_Purchase': dates,
'Sales': sales
})
# Convert to datetime (already datetime in our case)
sales_data['Date_of_Purchase'] = pd.to_datetime(sales_data['Date_of_Purchase'])
print("Data types:")
print(sales_data.dtypes)
Data types: Date_of_Purchase datetime64[ns] Sales int64 dtype: object
Setting Date as Index
Set the date column as index for time series plotting ?
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime, timedelta
# Create sample data
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='M')
sales = [100 + i*10 + np.random.randint(-20, 20) for i in range(len(dates))]
sales_data = pd.DataFrame({
'Date_of_Purchase': dates,
'Sales': sales
})
# Set date as index
sales_data = sales_data.set_index('Date_of_Purchase')
print("DataFrame with date index:")
print(sales_data.head())
DataFrame with date index:
Sales
Date_of_Purchase
2023-01-31 95
2023-02-28 118
2023-03-31 110
2023-04-30 150
2023-05-31 131
Creating the Uptrend Plot
Plot the data to visualize the uptrend pattern ?
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Create sample uptrend data
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='M')
sales = [100 + i*15 + np.random.randint(-10, 15) for i in range(len(dates))]
sales_data = pd.DataFrame({
'Date_of_Purchase': dates,
'Sales': sales
})
# Set date as index
sales_data = sales_data.set_index('Date_of_Purchase')
# Create the uptrend plot
plt.figure(figsize=(10, 6))
sales_data.plot(kind='line', color='blue', linewidth=2)
plt.title('Sales Data - Uptrend Pattern', fontsize=14)
plt.xlabel('Date')
plt.ylabel('Sales')
plt.grid(True, alpha=0.3)
plt.show()
[A line plot showing sales data with an upward trend from January to December 2023, with sales values increasing from around 100 to 250 over the time period]
Complete Example
Here's the complete code to plot an uptrend from a CSV file ?
import pandas as pd
import matplotlib.pyplot as plt
# Load data from CSV file
dataFrame = pd.read_csv("SalesRecords.csv")
print("Reading the CSV file...\n", dataFrame)
# Convert date column to datetime
dataFrame['Date_of_Purchase'] = pd.to_datetime(dataFrame['Date_of_Purchase'])
# Set date as index for time series plotting
dataFrame = dataFrame.set_index('Date_of_Purchase')
# Create the uptrend plot
plt.figure(figsize=(10, 6))
dataFrame.plot()
plt.title('Sales Records - Uptrend Analysis')
plt.xlabel('Date')
plt.ylabel('Values')
plt.show()
Key Points
- Convert date columns to datetime format using
pd.to_datetime() - Set the date column as index for proper time series plotting
- Use
plot()method to create line plots showing trends - Add titles and labels for better visualization
Conclusion
Plotting uptrends in Pandas involves converting date columns to datetime format, setting dates as index, and using the plot() method. This visualization helps identify increasing patterns in time series data effectively.
