Hi, I have following question: This question is about a plot as in the image bellow.
clear all;
close all;
x=(1:1:50);
y=x.^2;
plot(x,y,'-b');
hold on;
plot(x(1:5:end),y(1:5:end),'bo');
legend('data','Location', 'northeast');
Because the points are very close each other and I have multiple graphs I have to use markers. The problem is that I would like to have a legend showing not (data and data 2) but let say (data) which it combines both the marker and the line. Can be somehow overwritten the actual legend or create other new? Thank you
<<
>>

 Réponse acceptée

Star Strider
Star Strider le 8 Juil 2015
Modifié(e) : Star Strider le 8 Juil 2015

0 votes

If I understand correctly what you want to do, change this plot call:
plot(x, y,'-b');
hold on;
plot(x(1:5:end),y(1:5:end),'bo');
to simply:
plot(x, y, '-ob')
—————————————————————————————————————
EDIT: Changed to reflect ‘x’ and ‘y’ now appearing on both plot calls in the original Question.

4 commentaires

Ionut  Anghel
Ionut Anghel le 8 Juil 2015
Modifié(e) : Ionut Anghel le 8 Juil 2015
Sorry, It was my fault. The original picture look like this. So I want to have this legend in the figure up. In reality I have 200 graphs each having 10000 points. I have to plot 5 graphs in a figure and I cannot do it as in figure bellow because nobody will undestand anyting. So I plot marker not so often :plot(x,y,'-b'); hold on; plot(x(1:5:end),y(1:5:end),'bo'); legend('data','Location', 'northeast'); but the problem is that get different type of legend.
Star Strider
Star Strider le 8 Juil 2015
Modifié(e) : Star Strider le 8 Juil 2015
Apologise for the delay. Life intrudes.
This is a round-about way of doing what you want, but it works (in R2015a):
x = linspace(0, 50);
y = x.^2;
figure(1)
plot(x, y, '-b');
hold on
hp2 = plot(x(1:10:end), y(1:10:end), '-ob');
plot(x(1:10:end), y(1:10:end), 'ob')
hold off
legend(hp2, 'Data')
set(hp2, 'Visible','off')
It plots the data three times, first as a line, second as a line-marker combination, then with markers only. It then makes the line-marker combination invisible, while retaining it in the legend.
Plotting five sets of data on one axes and creating the legend entries for each of them is more complicated but straightforward. This plots two sets of data, but automatically expands to more, depending on the row-length of ‘y’ in this code:
x = linspace(0, 50);
y(1,:) = x.^2;
y(2,:) = sin(2*pi*x/25)*100+250;
cm = colormap(jet(5));
figure(1)
plot(x, y(1,:), '-', 'Color',cm(1,:));
hold on
for k1 = 1:size(y,1)
plot(x, y(k1,:), '-', 'Color',cm(k1,:));
hp(k1) = plot(x(1:10:end), y(k1,1:10:end), '-o', 'Color',cm(k1,:));
plot(x(1:10:end), y(k1,1:10:end), 'o', 'Color',cm(k1,:))
hl(k1) = legend(hp(k1), sprintf('Data %d',k1));
set(hp(k1), 'Visible','off')
end
hold off
legend(hp(1:size(y,1)))
Note: Here all vector (x,y) pairs are the same lengths. If yours are different lengths, you will have to alter the code appropriately to vary ‘x’ with each plot as well as ‘y’. If possible, make your data all row vectors of the same length (use interp1) so you will not need to alter this code any more than absolutely necessary.
The ‘cm’ assignment defines the colours for the plots. See the documentation on colormap if you want different colour definitions for each line.
EDIT — Added plot:
Ionut  Anghel
Ionut Anghel le 9 Juil 2015
Your solution is working very nice. Based on your code I create a function in a script which generates 200 figures with the desired legend. Thank you very much. I acceepted your previous answer because I cannot see how to accept your second answer. Best wishes.
Star Strider
Star Strider le 9 Juil 2015
My pleasure.
I only submitted one Answer, so you accepted it and all subsequent Comments. (Thank you!) This is how MATLAB Answers works, so you did it correctly. Sometimes it requires a few iterations to understand the problem and post a correct solution.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by