Plot line type not displaying as desired .

4 views (last 30 days)
I desire to create a plot with lines running through circular markers. For some reason, this does not plot as desired, but is registered correctly in the legend. I made an attempt with 'dummy variables ', whose limits were based on the limits of the actual variables I am working with, and that worked perfectly. Why does the plot not display as desired? A solution for this would be most appreciated. Attached is the script used.
% actual variables to be plotted
kf_max_SW_A = [ 0.0013 0.0049 0.0010 0.0011 0.0005 0.0007 ;
0.9845 1.0853 0.9962 0.9738 0.9454 0.9178 ] ;
Id_A = [ 51.6400 29.2000 61.3600 63.5000 77.2100 76.1200 ] ;
% dummy var xxx & yyy
xxx = linspace(20, 80, 5) ;
yyy = linspace(0*1e-3, 5*1e-3, 5) ;
% Plotting variables
% the required plot
plot( Id_A(1,1), kf_max_SW_A(1,1) , '-ok', 'LineWidth', 0.5, 'MarkerSize',5 , 'DisplayName', 'Max')
hold on ;
for i = 2 : length( kf_max_SW_A)
plot( Id_A(1,i), kf_max_SW_A(1,i) , '-ok', 'LineWidth', 0.5, 'MarkerSize',5 , 'HandleVisibility','off')
hold on ;
% the dummy plot
plot(xxx, yyy,'-or' ) ; hold off;
title(' Why do the variables for legend item Max not plot as desired ?')
legend('location','best') ;

Accepted Answer

Simon Chan
Simon Chan on 1 Jan 2022
Try the following to sort the x-axis in correct order:
[sort_Id_A,idx] = sort(Id_A);
sort_kf_max_SW_A = kf_max_SW_A(1,idx);
plot(sort_Id_A, sort_kf_max_SW_A , '-ok', 'LineWidth', 0.5, 'MarkerSize',5 , 'DisplayName', 'Max')
title('Is this your desired plot?')
legend('location','best') ;
  1 Comment
Eric Geraldo De-Lima
Eric Geraldo De-Lima on 1 Jan 2022
Thank you Simon. Your approach works perfectly for my purpose .
I just found a post with a somewhat similar issue as mine in which a contributor explained how the plot function behaves. That, " If you want to plot a line, the data need to be a single input to plot function instead of using a for loop, which would only plot your data points as individual points. " : My plot is not showing a line? Why? - ( It seems that's why my plot did not display as desired, and why your script gives the desired result.
Happy new year .

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by