Solve Matlab Legend Problem for Plotting Four Pairs of Time Series

  • MATLAB
  • Thread starter Old Guy
  • Start date
  • Tags
    Matlab
In summary, the person is having trouble with the legend entries in their plot, as the first two legend items have red lines and the second two have black lines, making it difficult to differentiate between the pairs of lines. They are looking for a solution to make each legend entry match the color of the corresponding pair of lines on the plot. The provided code is not sufficient to generate the plot, and it is suggested to change the line styles or refer to the documentation for further help.
  • #1
Old Guy
103
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
  • #2
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
 

1. What is the purpose of using the legend in a MATLAB plot?

The legend in a MATLAB plot is used to label different data series or plots within a single figure. This allows for easy identification and comparison of the data.

2. How do I add a legend to my MATLAB plot?

To add a legend to a MATLAB plot, you can use the "legend" function and specify the labels for each data series. You can also customize the appearance of the legend, such as its location and font size.

3. Why am I encountering problems when trying to plot four pairs of time series in MATLAB?

There could be several reasons for encountering problems when plotting four pairs of time series in MATLAB. Some common issues include incorrect data formatting, incorrect use of plotting functions, and overlapping data. It is important to carefully check your code and data to identify and resolve any potential issues.

4. How can I troubleshoot the legend problem in MATLAB when plotting four pairs of time series?

To troubleshoot the legend problem in MATLAB, you can start by checking your code and data for any errors. You can also try adjusting the legend settings, such as the location and orientation, to see if that resolves the issue. Additionally, you can consult MATLAB documentation or seek help from online forums or communities.

5. Are there any alternative methods for labeling and identifying data in a MATLAB plot besides using the legend?

Yes, there are alternative methods for labeling and identifying data in a MATLAB plot. You can use the "text" function to add labels or annotations directly onto the plot. You can also use the "title" function to add a title to the plot, and the "xlabel" and "ylabel" functions to label the axes. Additionally, you can use different colors, line styles, or markers to differentiate between data series without using a legend.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top