Solve Matlab Legend Problem for Plotting Four Pairs of Time Series

  • Context: MATLAB 
  • Thread starter Thread starter Old Guy
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion addresses a common issue in MATLAB plotting where multiple lines share the same color, leading to confusion in the legend. The user is attempting to plot four pairs of time series with distinct colors for each pair but encounters overlapping colors in the legend. The solution involves modifying the line styles in addition to colors, using commands like plot(...,'--k') for dashed lines and plot(...,'-ok') for circle markers. This approach ensures that each legend entry corresponds accurately to its respective line on the plot.

PREREQUISITES
  • Familiarity with MATLAB plotting functions
  • Understanding of line specifications in MATLAB
  • Basic knowledge of time series data representation
  • Experience with MATLAB's legend functionality
NEXT STEPS
  • Explore MATLAB line specifications in detail using MATLAB documentation
  • Learn how to customize legends in MATLAB plots
  • Investigate advanced plotting techniques for time series data in MATLAB
  • Experiment with different line styles and markers in MATLAB for better data visualization
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers who are working with time series data and require effective visualization techniques to distinguish between multiple data series in plots.

Old Guy
Messages
101
Reaction score
1
I am trying to make a plot of four pairs of time series. The plot appears fine; I have two lines in each color as I want. The problem, in this case, is that the first two legend items both have red lines, and the second two both have black lines. I want each legend entry to match the color of the pair of lines on the plot. My code is below; I hope someone can help. Some comments on the code:

z is the counter - I have a loop that calculates the data and puts in into plotdata for each run. The four correspond to four values of p.

hold on
figure(1)
colors=['r' 'k' 'g' 'b'];
semilogy(plotdata(:,1),plotdata(:,2:3),colors(z))
xlabel('Time, {\itt}','FontSize',24,'FontName','Times New Roman')
ylabel('Fractional Population','FontSize',24,'FontName','Times New Roman')
set(gca,'FontSize',24,'FontName','Times New Roman')
grid on
legend(char({strcat('{\itp} =',num2str(p(1:z)'))}))

Thanks!
 
Physics news on Phys.org
If you had attached the plot, it would have been better. The code provided is not sufficient to generate the plot either.

What I understood is, you have same colour for two different lines, and then you can't make it out from the legend which one is which. Just change the line styles. Plot one with plot(...,'k'), another with plot(...,'--k'), and yet another with plot(...,'-ok'). You can put the same into the colors matrix and suitably modify your code as needed.

Check this documentation too:
https://in.mathworks.com/help/matlab/ref/linespec.html
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K