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 a time as an index value in a Pandas dataframe in Matplotlib?
To plot a time as an index value in a Pandas DataFrame using Matplotlib, you need to set the time column as the DataFrame index. This allows the time values to appear on the x−axis automatically when plotting.
Steps
Create a DataFrame with time and numeric data columns
Convert the time column to datetime format if needed
Set the time column as the DataFrame index using
set_index()Use the DataFrame's
plot()method to create the visualization
Basic Time Series Plot
Here's how to create a simple time series plot with time as the index ?
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True
# Create DataFrame with time and speed data
df = pd.DataFrame({
'time': pd.date_range("2021-01-01 12:00:00", periods=10, freq='H'),
'speed': np.linspace(1, 10, 10)
})
print("Original DataFrame:")
print(df.head())
# Set time as index and plot
df.set_index('time').plot(kind='line', marker='o')
plt.title('Speed vs Time')
plt.ylabel('Speed')
plt.show()
Original DataFrame:
time speed
0 2021-01-01 12:00:00 1.0
1 2021-01-01 13:00:00 2.0
2 2021-01-01 14:00:00 3.0
3 2021-01-01 15:00:00 4.0
4 2021-01-01 16:00:00 5.0
Multiple Time Series
You can plot multiple columns against the time index ?
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Create DataFrame with multiple metrics
df = pd.DataFrame({
'time': pd.date_range("2021-01-01", periods=30, freq='D'),
'temperature': 20 + 5 * np.sin(np.linspace(0, 4*np.pi, 30)) + np.random.normal(0, 1, 30),
'humidity': 60 + 10 * np.cos(np.linspace(0, 4*np.pi, 30)) + np.random.normal(0, 2, 30)
})
# Set time as index
df_indexed = df.set_index('time')
# Plot multiple columns
df_indexed.plot(subplots=True, figsize=(10, 8))
plt.suptitle('Weather Data Over Time')
plt.show()
[Displays two subplot graphs showing temperature and humidity trends over 30 days]
Customizing Time Axis
You can customize the time axis formatting and labels ?
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.dates import DateFormatter
# Create sample data
df = pd.DataFrame({
'time': pd.date_range("2021-01-01", periods=100, freq='D'),
'value': np.cumsum(np.random.randn(100))
})
# Set time as index
df_indexed = df.set_index('time')
# Create plot with custom formatting
fig, ax = plt.subplots(figsize=(12, 6))
df_indexed.plot(ax=ax, color='blue', linewidth=2)
# Format x-axis
ax.xaxis.set_major_formatter(DateFormatter('%m/%d'))
plt.xticks(rotation=45)
plt.title('Time Series with Custom Date Formatting')
plt.tight_layout()
plt.show()
[Displays a line plot with custom date formatting on x-axis showing MM/DD format]
Key Points
set_index('time')converts the time column to the DataFrame indexThe DataFrame's
plot()method automatically uses the index as x−axisUse
subplots=Trueto create separate plots for multiple columnsPandas automatically handles time−based x−axis labeling
You can customize date formatting using
DateFormatterfrom matplotlib
Conclusion
Setting time as a DataFrame index enables automatic time−based plotting in Pandas. Use set_index() followed by plot() for quick time series visualization, and customize with matplotlib for advanced formatting needs.
