The scale of the x-axis (Format: HH:MM:SS)

I have 2 imported columns from excel that I plotted already, the time column ( the format HH:MM:SS) is on the x-axis and the value column is on the y-axis.
The problem that I am trying to solve since both the columns are 1645 long (or better said: they contain 1645 rows), so I want to change the x axis scale on the graph to make the distance between the times somehow longer, otherwise it looks like a long unwanted line under the x-axis.
Any Ideas how to change the x-axis scale on the graph for a better appearance?
The second hurdle that I am facing, I keep getting the error below whenever I try to plot the average line of the value column on the same graph where the time and the value column are plotted. Maybe because the x and y axis are already used?
avgline= sum(value_column)/size(value_column),1); %the value_column and time column are already defined from the workspace)
nexttile
plot(time_column,value_column)
hold on
plot(avgline,'-r','LineWidth',2);
title('Value 6')
hold off
%The Error that I get:
%Values plotted against x-axis must be categorical values. To create categorical values, use the categorical function.

 Respuesta aceptada

Shae Morgan
Shae Morgan el 10 de Ag. de 2020
Editada: Shae Morgan el 10 de Ag. de 2020
try looking into the 'xtick' property to specify which x-ticks you want to show. it'll suppress the rest of them
x=1:10; %demo time values
y=1:10; %demo data
plot(x,y) %create figure
set(gca,'xtick',[1 3 6 10]) %manipulate x-axis to only show some values

10 comentarios

Ramo Rafsel
Ramo Rafsel el 10 de Ag. de 2020
Editada: Ramo Rafsel el 10 de Ag. de 2020
I just added the last row of your code:
set(gca,'xtick',[13:10:58 13:20:58 13:30:51 13:40:39 13:49:14 ])
and matlab didn't really get it, I am sure I did not write it right, but I want these time intervalls to be shown out of the 1645 time data.
Shae Morgan
Shae Morgan el 10 de Ag. de 2020
you'll likely need to conver the times you want into a number and then plot that number on the x-axis. Without an error code it's hard to tell what you mean by "Matlab didn't really get it"
here is a function that should help:
If you provide a better error code, or perhaps a sample of your data, it'll be easier to provide you with a solution
Ramo Rafsel
Ramo Rafsel el 13 de Ag. de 2020
Editada: Ramo Rafsel el 13 de Ag. de 2020
@Shae Morgan Thanks a lot in advance for your help
This is the code, v5 and time5 are the extracted variables from the imported excel file (variable 5) that contains the value and the time column, check the picture I added and see how the line under the x axis looks like, that's what I would like to change.
v5=variable5.value; %whereas variable5 is the imported excel file
time5=variable5.time; %and value and time are the two column where time is for the x axis and value for the y axis
avg5 = sum(v5)/size(v5,1); %the average line of the column v5
nexttile
plot(time5,value5);
title('value 5')
nexttile
plot( [0 size(v5,1)],[avg5 avg5], 'y-', 'LineWidth',2);
title('Average line of the variable 5')
I also would like to plot the average line of the value column (avg5) on the same plot of the figure where the time5 and value5 are plotted. I tried the following codes but it was no avail:
%trial code 1:
nexttile
plot(time5,value5);
hold on
plot([0 size(v5,1)],[avg5 avg5], 'y-', 'LineWidth',2);
hold off
title('value 5')
%trial code 2:
plot(time5,value5,time5, avg5,'b-','LineWidth',2);
%trial code 3:
plot([0 size(time5,1)],[avg5 avg5],'b-','LineWidth',2);
Shae Morgan
Shae Morgan el 13 de Ag. de 2020
What is the variable type of your x values? are they datetime variables?
Shae Morgan
Shae Morgan el 13 de Ag. de 2020
Editada: Shae Morgan el 13 de Ag. de 2020
Try this out: Since I didn't have your variable class, I had to assume you're dealing with time-stamps here. This works for those:
t1 = datetime(2020,8,12,13,11,24); %start time
t2 = datetime(2020,8,12,13,18,36); %end time
time5 = t1:seconds:t2; %create a vector of time points from the start time to the end time
v5 = randn(size(time5)); %create random y-axis data
plot(time5,v5) %plot data
hold on; %hold the current figure so the first plot doesn't erase when you do the 2nd plot
avg5 = mean(v5); %the average value of v5
x2= [time5(1) time5(end)]; %x-values for the average line
plot(x2,[avg5 avg5], 'y-', 'LineWidth',2); %plot the avg line
title('value 5 by time plot') %title the plot
legend('Raw data','Average line') %add a legend to differentiate the two data sources
ax = gca; %get current axis
x_step_size=100; %step size between labels in seconds
ax.XAxis.TickValues = t1:seconds(x_step_size):t2; %adjust the tick values to the correct spacing
Shae Morgan
Shae Morgan el 13 de Ag. de 2020
Just change the x_step_size to whatever step size you want in seconds (or in minutes if you'd rather do that - just change the function "seconds" to "minutes" in the line below that.
Shae Morgan
Shae Morgan el 13 de Ag. de 2020
Ramo Rafsel
Ramo Rafsel el 14 de Ag. de 2020
Editada: Ramo Rafsel el 14 de Ag. de 2020
@Shae Morgan Thanks a lot for your help. you gave me a solution for the time stamp problem. the x values are the time in HH:MM:SS format, and it is categorical. If I correctly understood your question about the variable type of the x values.
One thing that I couldnt understand by comparing the picture that I added and your plot, my y-axis has another scale. While yours is between 4 and -4.
I tried using the value column v5 instead of time5 because I wanted to plot the value column on the y axis. But I get a figure with two lines.
And regarding this line and also t1 and t2 variables (I added the two variables with the first and last hour (in HH:MM:SS), as you did), do I have to create random y-axis data, when I already have the value column that contains the data for the y-axis?
v5 = randn(size(v5)); %create random y-axis data
you don't have to make up random data to use for your y-axis. Just use the variable you have. You didn't provide any of your data, so I had to make-up numbers for the y and x axes to demonstrate the fix for the x-axis. You substitute those lines of code for what you already have and it should work.
%You don't need this code if you already have a time5 and v5 variable
%I added this code in order to demonstrate the fix
t1 = datetime(2020,8,12,13,11,24); %start time
t2 = datetime(2020,8,12,13,18,36); %end time
time5 = t1:seconds:t2; %create a vector of time points from the start time to the end time
v5 = randn(size(time5)); %create random y-axis data
% --------------^^^^^----- don't use this code if you already have your time5 and v5 variables!
%end set-up section
%use this code with your original time5 and v5 data that you already have stored as variables
plot(time5,v5) %plot data
hold on; %hold the current figure so the first plot doesn't erase when you do the 2nd plot
avg5 = mean(v5); %the average value of v5
x2= [time5(1) time5(end)]; %x-values for the average line
plot(x2,[avg5 avg5], 'y-', 'LineWidth',2); %plot the avg line
title('value 5 by time plot') %title the plot
legend('Raw data','Average line') %add a legend to differentiate the two data sources
ax = gca; %get current axis
x_step_size=100; %step size between labels in seconds
ax.XAxis.TickValues = t1:seconds(x_step_size):t2; %adjust the tick values to the correct spacing

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Discrete Data Plots en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Ag. de 2020

Editada:

el 18 de Ag. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by