- Using ‘datenum’ function to convert date and time to numeric: https://in.mathworks.com/help/matlab/ref/datetime.datenum.html
- Plot scatter graphs using “scatter” function: https://in.mathworks.com/help/matlab/ref/scatter.html
- View and set current colormap: https://in.mathworks.com/help/matlab/ref/colormap.html
Scatter plot with timescale colormap
13 views (last 30 days)
Show older comments
Hello everyone,
I have a datasets containing vx, vy, and t date time evolution in excel date format dd/mm/yyyy hh:mm format (see attached dataset). Anyone knows to present this datasets into scatter plot of vx, vy with colormap evolution of time?
My intention is getting scatter plot of vx vs vy with colormap more or less like this:
Thank you!
0 Comments
Accepted Answer
Rahul
on 11 Oct 2024
Edited: Rahul
on 14 Oct 2024
In order to import an excel spreadsheet and create a scatter plot of timeseries data, involving two variables along with a heatmap based on values of another array.
You can import ‘xlsx’ data using ‘readtable’ function and later convert the ‘datetime’ values stored in ‘t’ column to double data type using ‘datenum’ function, using the following steps:
data = readtable('data_UVT.xlsx');
x = data.vx
y = data.vy
t = datenum(data.t)
Further, a scatter plots can be created using filled face color style, which can be later customized using color profiles by ‘colormap’ function to create a heatmap:
% Create scatter plot
scatter(x, y, 10, t, 'filled'); % 100 is the size of the dots, 'filled' for solid dots
% Add color bar to show the time step mapping
colorbar;
% Add labels
xlabel('X Data Samples (vx)');
ylabel('Y Data Samples (vy)');
title('Scatter Plot with Time-based Color Change');
% Use a colormap (e.g., 'jet', 'parula', etc.)
colormap('jet'); % You can change to 'parula', 'hot', etc.
% Optionally, adjust axis limits if needed
axis tight;
% Customize the color bar to show datetime instead of numeric values
num_ticks = 4; % Number of ticks you want on the colorbar
tick_values = linspace(min(t_numeric), max(t_numeric), num_ticks); % Get the tick positions in numeric form
% Convert the tick positions back to datetime
tick_labels = datestr(tick_values, 'yyyy-mm-dd HH:MM:SS'); % Customize the datetime format as needed
% Apply the tick labels to the colorbar
c.Ticks = tick_values; % Set the tick positions on the colorbar
c.TickLabels = tick_labels; % Set the tick labels as datetime
For more information regarding the functions mentioned above, refer to the following documentation links:
3 Comments
Rahul
on 11 Oct 2024
Hey @Adi Purwandana, you can set x-ticks labels and format back to datenum, after the applying the heatmap, using your specified 'datetime' fomat using the properties of the colorbar. Here's how:
data = readtable('data_UVT.xlsx')
x = data.vx;
y = data.vy;
t = data.t;
% Convert datetime to serial date number for plotting
t_numeric = datenum(t);
% Create scatter plot with the numeric time as the color
scatter(x, y, 10, t_numeric, 'filled');
c = colorbar;
xlabel('X Data Samples');
ylabel('Y Data Samples');
title('Scatter Plot with Time-based Color Change');
colormap('jet');
axis tight;
% Customize the color bar to show datetime instead of numeric values
num_ticks = 4; % Number of ticks you want on the colorbar
tick_values = linspace(min(t_numeric), max(t_numeric), num_ticks); % Get the tick positions in numeric form
% Convert the tick positions back to datetime
tick_labels = datestr(tick_values, 'yyyy-mm-dd HH:MM:SS'); % Customize the datetime format as needed
% Apply the tick labels to the colorbar
c.Ticks = tick_values; % Set the tick positions on the colorbar
c.TickLabels = tick_labels; % Set the tick labels as datetime values
More Answers (0)
See Also
Categories
Find more on Colormaps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!