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
How to plot Timeseries based charts using Pandas?
Time series data consists of data points collected at regular time intervals, such as weather data, stock prices, or ECG reports. Visualizing this temporal data through charts is crucial for identifying trends, patterns, and making predictions. Pandas provides excellent tools for plotting time series data with just a few lines of code.
Prerequisites and Setup
First, install the required packages ?
pip install pandas matplotlib
Import the necessary libraries ?
import pandas as pd import matplotlib.pyplot as plt
Loading Time Series Data
You can load time series data from various sources like CSV files, APIs, or databases. When loading from a CSV file, use the parse_dates parameter to automatically parse date columns ?
data = pd.read_csv('data.csv', parse_dates=['timestamp_column'])
Setting Up the Time Index
For proper time series handling, set the timestamp column as the DataFrame index ?
data.set_index('timestamp_column', inplace=True)
Creating Sample Data
Let's create a sample dataset to demonstrate different plotting techniques ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample time series data
ts_data = {
'Date': ['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
'2022-05-01', '2022-06-01', '2022-07-01', '2022-08-01',
'2022-09-01', '2022-10-01'],
'Sales': [302, 404, 710, 484, 641, 669, 897, 994, 1073, 944],
'Revenue': [849, 1488, 912, 855, 445, 752, 699, 1045, 1232, 974],
'Profit': [715, 355, 284, 543, 112, 1052, 891, 776, 924, 786]
}
df = pd.DataFrame(ts_data)
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('Date')
print(df.head())
Sales Revenue Profit
Date
2022-01-01 302 849 715
2022-02-01 404 1488 355
2022-03-01 710 912 284
2022-04-01 484 855 543
2022-05-01 641 445 112
Line Charts
Line charts are ideal for showing trends over time. They connect data points with lines to reveal patterns ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
ts_data = {
'Date': ['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
'2022-05-01', '2022-06-01', '2022-07-01', '2022-08-01',
'2022-09-01', '2022-10-01'],
'Sales': [302, 404, 710, 484, 641, 669, 897, 994, 1073, 944],
'Revenue': [849, 1488, 912, 855, 445, 752, 699, 1045, 1232, 974]
}
df = pd.DataFrame(ts_data)
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('Date')
# Create line plot
df.plot(figsize=(10, 6))
plt.title('Time Series Line Chart')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.tight_layout()
plt.show()
Bar Charts
Bar charts work well for comparing discrete time periods or categorical data over time ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
ts_data = {
'Date': ['Q1', 'Q2', 'Q3', 'Q4'],
'Sales': [1416, 1794, 2590, 1918]
}
df = pd.DataFrame(ts_data)
df = df.set_index('Date')
# Create bar chart
df.plot(kind='bar', figsize=(8, 6), color='skyblue')
plt.title('Quarterly Sales')
plt.xlabel('Quarter')
plt.ylabel('Sales')
plt.xticks(rotation=0)
plt.tight_layout()
plt.show()
Area Charts
Area charts show the magnitude and proportion of different variables over time by filling the area under the lines ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
ts_data = {
'Date': pd.date_range('2022-01-01', periods=6, freq='M'),
'Product_A': [300, 450, 600, 500, 700, 650],
'Product_B': [200, 300, 400, 350, 450, 400]
}
df = pd.DataFrame(ts_data)
df = df.set_index('Date')
# Create area plot
df.plot(kind='area', figsize=(10, 6), alpha=0.7)
plt.title('Product Sales Over Time')
plt.xlabel('Date')
plt.ylabel('Sales')
plt.tight_layout()
plt.show()
Customizing Time Series Plots
Pandas and Matplotlib offer extensive customization options for styling your charts ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
dates = pd.date_range('2022-01-01', periods=10, freq='M')
values = [100, 120, 140, 110, 160, 180, 200, 170, 220, 240]
df = pd.DataFrame({'Value': values}, index=dates)
# Create customized plot
df.plot(figsize=(12, 6),
linewidth=3,
linestyle='-',
marker='o',
markersize=8,
color='darkblue')
plt.title('Customized Time Series Plot', fontsize=16, fontweight='bold')
plt.xlabel('Date', fontsize=12)
plt.ylabel('Value', fontsize=12)
plt.grid(True, alpha=0.3)
plt.legend(['Data Series'], loc='upper left')
plt.tight_layout()
plt.show()
Chart Type Comparison
| Chart Type | Best For | Key Feature |
|---|---|---|
| Line Chart | Continuous trends | Shows patterns over time |
| Bar Chart | Categorical/discrete data | Easy comparison between periods |
| Area Chart | Multiple series comparison | Shows proportion and magnitude |
| Scatter Plot | Correlation analysis | Reveals relationships between variables |
Conclusion
Pandas provides powerful and flexible tools for visualizing time series data through various chart types. Line charts excel at showing trends, bar charts work well for categorical comparisons, and area charts effectively display proportions over time. With proper customization, these visualizations become powerful tools for data analysis and decision-making.
