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

조회 수: 8 (최근 30일)
Ramo Rafsel
Ramo Rafsel 2020년 8월 10일
편집: Ramo Rafsel 2020년 8월 18일
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.

채택된 답변

Shae Morgan
Shae Morgan 2020년 8월 10일
편집: Shae Morgan 2020년 8월 10일
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
Ramo Rafsel
Ramo Rafsel 2020년 8월 14일
편집: Ramo Rafsel 2020년 8월 14일
@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
Shae Morgan
Shae Morgan 2020년 8월 14일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by