set number of ticks at any given time

hi,
i am searching for something to set the amunt of ticks to a fix value of a plot.
the reason is, right now im plotting live data and the plot constantly changes the number of ticks and therefore the space inbetween, wich makes it harder to read than necessary.
now i know that i can change XTick to values of my liking, but when i do, i also have to change them all the time in the loop, so my tought was to simply change the amount of xticks. however, i couldnt find a solution. i am using animatedline, does that make a difference?
sincerely
Andre

 Respuesta aceptada

If you know the bounds over which your data to be plotted spans, I would set those limits first using axis or xlim and ylim (and set the ticks and tick labels with xticks, xticklabels, etc.) then addpoints to your animatedline. Doing so means that the axes will not need to recalculate its limits to include the new points and so the ticks won't need to change during the plotting.
Run the following two sections of code and watch the plotting. The first doesn't need to change the limits at all once the axes is created by the axis call. The second updates the limits with each new point. [The effect in MATLAB Answers is the same, since it only shows the final results, but in an interactive MATLAB session you will be able to see the difference.]
With axes limits set initially:
x = 0:10;
y = x.^2;
[minX, maxX] = bounds(x);
[minY, maxY] = bounds(y);
axis([minX maxX minY maxY])
h = animatedline(NaN, NaN);
for k = 1:numel(x)
addpoints(h, x(k), y(k));
pause(1)
end
Without axes limits set initially:
figure
h = animatedline(NaN, NaN);
for k = 1:numel(x)
addpoints(h, x(k), y(k));
pause(1)
end

4 comentarios

Andre
Andre el 20 de Jun. de 2023
thanks for your long answer.
i think i didnt do a good job explaining my problem.
my plot starts at 0 to 10, like your examples, but i get more and more data. i only display 11 datapoints, which keep progressing tho. with the next datapoint the plot is from 1 to 11. while that sound trivial, matlab for some reason constantly changes the "displaying window". i mean by that the value from minimum to maximum, in your examples 10, in my example 10 as well.
matlab changes that, sometimes its 5 or 6 and then its 10. its also chaning kinda fast. which is irritating. so i wanna calm the plot a bit down. my first way was to find some value where i can set the amount of ticks displayed. not set the ticks themself.
however, the 2 answers i got suggest that isnt possible.
Do you know the maximum value that your data will have? For example, in the example I posted if you knew that you wanted the plot to show from y = 0 to y = 100:
h = animatedline(NaN, NaN, Marker = 'o');
xlim([0 20])
ylim([0 100])
for k = 0:20
newY = randi([0 100]); % Random integer from 0 to 100
addpoints(h, k, newY)
pause(0.25)
end
Note that the axes limits never changed, even though I may not have generated data with y = 0 or y = 100.
For what you're asking there is no way to specify a number of ticks without also specifying the locations of the ticks. Even if there was, you'd still have the same problem with MATLAB recalculating where the ticks were located as the axes limits changed.
If you have a fixed set of ticks that you want to remain regardless of how the axes limits change, set the XTickMode property of the axes to 'manual' instead of 'auto'.
figure
axes(XTickMode = 'manual')
xticks(0:0.25:1)
h = animatedline(NaN, NaN, Marker = 'o');
for k = 0:20
newY = randi([0 100]); % Random integer from 0 to 100
addpoints(h, k, newY)
pause(0.25)
end
Andre
Andre el 21 de Jun. de 2023
it has a "pseudo" maximum. its in a for loop and each loop, data is being collected. and i have the same problem as in the last example, its just not at the beginning and instead at the end.
when i want to collect like 1000 data points, i start with like a range from 0 to 20, but i would end with a range of 980 to 1000. so that i always have 21 values displayed. maybe it would be fine with matlab always recalculating where the ticks are located, but at least the number would variate that drastically. i also limit the values in animatedline, so i dont have a plot with thousands and thousands of values. maybe that was the wrong aproach. i thought that it may slows down, when i collect a 100k data points each for three sensors, having 300k data-sets in the plots.
anyways, bottom line is, what i want is not possible, unless i also set the ticks each loop again. then i will look how fast that is. thanks for going through that whole topic :)
Andre
Andre el 21 de Jun. de 2023
im a little lost right, i made a new script where i can test it out, so i dont have to screw around in the script i am using at the end. after i did it, i let it run and i do not have the same effect, it is working there as i want it to work.
so im not sure whats causing it.

Iniciar sesión para comentar.

Más respuestas (1)

Richard Burnside
Richard Burnside el 20 de Jun. de 2023

0 votos

The "xticks" and "yticks" command will let you manually set your tick values.

1 comentario

Andre
Andre el 20 de Jun. de 2023
Editada: Andre el 20 de Jun. de 2023
but thats exactly what i dont want to do. my goal is to have it always have 10 ticks, and not some auto-stuff interfere and change it with each loop iteration, but i cant find such a thing.
and thanks :)

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Productos

Versión

R2023a

Preguntada:

el 20 de Jun. de 2023

Comentada:

el 21 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by