how to plot all zeros on xaxis?

9 Ansichten (letzte 30 Tage)
nines
nines am 1 Apr. 2025
Kommentiert: Mathieu NOE am 1 Apr. 2025
Hello,
I have the following code:
figure; plot(x_mm, adcValues)
Where the values look like:
adcValues x_mm
_________ _____
0.041117 -0.9
0.034457 -0.45
0 0
0 0
0 0
0 0
0 0
0 0
0.023807 0.45
0.030902 0.9
0.032225 1.35
and the plot looks like:
where you can see that the 6 zeros are being collapsed into one zero.
I instead want something like the red line where you are getting a step function at the first adcvalue that is not at zero:
Where the zeros are not all collapsed but instead visualized on the plot.
Thanks so much.

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 1 Apr. 2025
hello
this could be one solution, but it's very basic (for the moment) as all x points are displayed with the same spacing , whatever their true numerical values are
this can be refined if needed (?)
x_mm = [-0.9
-0.4
0
0
0
0
0
0
0.45
0.9
1.35];
adcValues = [0.041117
0.034457
0
0
0
0
0
0
0.023807
0.030902
0.032225];
indXzero = abs(x_mm)<eps;
xlabels = num2str(x_mm);
plot(1:numel(x_mm),adcValues,'-*');
set(gca,'XTick',1:numel(x_mm),'XTickLabel',xlabels);
  1 Kommentar
Mathieu NOE
Mathieu NOE am 1 Apr. 2025
this is a refined version - you can choose how wide is your "zero" x range - and the other non zero x values are displayed with the correct spacing
x_mm = [-0.9
-0.4
0
0
0
0
0
0
0.45
0.9
1.35];
adcValues = [0.041117
0.034457
0
0
0
0
0
0
0.023807
0.030902
0.032225];
% create new x vector
indXzero = find(abs(x_mm)<eps);
xspacing_zero = 0.5; % total x span for all zero values
xspacing_zero_array = linspace(-xspacing_zero/2,xspacing_zero/2,numel(indXzero));
x_mm_new = x_mm;
x_mm_new(indXzero) = xspacing_zero_array;
% shift left hand side non zero x values (to have correct spacing)
indleft = (1:min(indXzero)-1);
x_mm_new(indleft) = x_mm(indleft) + xspacing_zero_array(1);
% shift right hand side non zero x values (to have correct spacing)
indright = (max(indXzero)+1:numel(x_mm));
x_mm_new(indright) = x_mm(indright) + xspacing_zero_array(end);
% plot
xlabels = num2str(x_mm); % plot "true" x_mm values at the specified ticks positions
plot(x_mm_new,adcValues,'-*');
set(gca,'XTick',x_mm_new,'XTickLabel',xlabels);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Les Beckham
Les Beckham am 1 Apr. 2025
Bearbeitet: Les Beckham am 1 Apr. 2025
This data is not the same as what your plot shows. Looks like maybe you left out some points at the beginning and the end of the data.
Nevertheless, note that all six of the adcValues are located at x_mm location equal to zero. So the middle of the plot does reflect this data correctly.
data = ...
[ 0.041117 -0.9
0.034457 -0.45
0 0
0 0
0 0
0 0
0 0
0 0
0.023807 0.45
0.030902 0.9
0.032225 1.35 ];
If you want the plot to be flat at zero between -0.45 and 0.45, you need to provide x_mm data at those points. Perhaps this will do what you want.
adcValues = data(:,1);
x_mm = data(:,2);
x_mm(3) = x_mm(2); % add a zero adcValue at -0.45
x_mm(8) = x_mm(9); % add a zero adcValue at +0.45
plot(x_mm, adcValues, 'o-')
grid on

Kategorien

Mehr zu Polar Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by