I am creating an XY plot of two values and shading them based on timestamps. I am trying to edit the labels/ticks on the colorbar, but when I edit the labels I either lose the colorbar, the labels dont come through correctly or a mixture of both. Ideally, I want to create ~6 ticks based on the start and end time of the data I am plotting.
code I am currently using:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
# Generate a range of timestamps
start_date = '2023-01-01'
end_date = '2023-01-31'
timestamps = pd.date_range(start=start_date, end=end_date, freq='h') # hourly frequency
# Generate random numbers
np.random.seed(0) # for reproducibility
random_numbers1 = np.random.rand(len(timestamps))
random_numbers2 = np.random.rand(len(timestamps))
# Create a DataFrame with the generated data
df = pd.DataFrame({
'Timestamp': timestamps,
'RandomValue1': random_numbers1,
'RandomValue2': random_numbers2
})
# Convert the Timestamp column to datetime format
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
# Create the plot
plt.figure(figsize=(10, 6))
sc = plt.scatter(df['RandomValue1'], df['RandomValue2'], c=df['Timestamp'], cmap='viridis', alpha=0.5)
cbar = plt.colorbar(sc, label='Time')
plt.xlabel('Random Values')
plt.ylabel('Random Values')
plt.title(f'Plot of Random Values shaded by Time')
# Convert timestamps to numerical values
num_timestamps = mdates.date2num(df['Timestamp'])
# Customize the colorbar ticks and labels to reflect timestamps
num_ticks = 6 # Number of ticks you want on the colorbar
ticks = pd.date_range(start=df['Timestamp'].min(), end=df['Timestamp'].max(), periods=num_ticks)
cbar.set_ticks(mdates.date2num(ticks))
cbar.set_ticklabels([tick.strftime('%Y-%m-%d %H:%M') for tick in ticks])
plt.show()
Try out this updated code. This code creates a well-labelled colorbar with approximately 6 evenly spaced ticks corresponding to their timestamps. Let me know if it helps.
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
# Generate a range of timestamps
start_date = '2023-01-01'
end_date = '2023-01-31'
timestamps = pd.date_range(start=start_date, end=end_date, freq='h') # hourly frequency
# Generate random numbers
np.random.seed(0) # for reproducibility
random_numbers1 = np.random.rand(len(timestamps))
random_numbers2 = np.random.rand(len(timestamps))
# Create a DataFrame with the generated data
df = pd.DataFrame({
'Timestamp': timestamps,
'RandomValue1': random_numbers1,
'RandomValue2': random_numbers2
})
# Convert the Timestamp column to datetime format
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
# Create the plot
plt.figure(figsize=(10, 6))
sc = plt.scatter(df['RandomValue1'], df['RandomValue2'], c=mdates.date2num(df['Timestamp']), cmap='viridis', alpha=0.5)
cbar = plt.colorbar(sc, label='Time')
plt.xlabel('Random Values')
plt.ylabel('Random Values')
plt.title('Plot of Random Values shaded by Time')
# Customize the colorbar ticks and labels
num_ticks = 6 # Number of ticks you want on the colorbar
ticks = pd.date_range(start=df['Timestamp'].min(), end=df['Timestamp'].max(), periods=num_ticks)
tick_positions = mdates.date2num(ticks) # Convert to numerical positions
cbar.set_ticks(tick_positions)
cbar.set_ticklabels([tick.strftime('%Y-%m-%d %H:%M') for tick in ticks])
plt.show()
How this code works:
The code generates 6 evenly spaced datetime ticks using pd.date_range
. mdates.date2num()
converts these ticks to numerical values and stores in the variable tick_positions
. cbar.set_ticks(tick_positions)
sets the colorbar ticks and cbar.set_ticklabels()
applies the formatted labels.
Basically: The timestamps are turned into numbers so they can be used in the plot, and the colorbar is set up with evenly spaced markers that show the times in a clear, readable format. This makes it easy to match the colorbar to the data and understand what it represents.