Trying to plot lines in scatterplot, a loop duplicates legend entries, a single plot command gives no image.

5 ビュー (過去 30 日間)
I have a scatter plot with horizontal bars for the average value in them.
I have three arrays with data, 1 categorical and 2 with double values.
plotData_X is a 100x1 categorical with values of:
1
1
1
2
2
2
3
...
And so forth.
The plotData_X values come from a tabe that uses strings as value, Table_Data_1.Position , but I couldn't find a way to plot the scatterplot with string values for the x-axis at all.
So I created the plotData_X as categorical:
plotData_X=categorical(Table_Data_1.Position,positionToPlotData1);
with positionToPlotData1 being a string array containing strings from "1" to "n" for each x-axis position that I want to plot.
plotData_Y has the same 100x1 dimensions and contains double values:
NaN
NaN
NaN
10.0
15.0
20.0
...
And so forth. plotData_Y_avg contains average values for each point in X:
NaN
NaN
NaN
15.0
NaN
NaN
...
Now plot plotData_X and plotData_Y as a scatterplot:
scatter(plotData_X,plotData_Y,20,colourValue,"x","LineWidth",0.7,"DisplayName",nameDataset1);
Which gives me a scatter plot. Now I want to add the plotData_Y_avg for each X position as a horizontal bar:
My attempt was to loop through the size of one array and plot it like this:
for i=1 : 1 : size(plotData_X,1)
plot([double(plotData_X(i))+[-0.4 0.4]],[plotData_Y_avg(i) plotData_Y_avg(i)],"Color",colourValue1,"LineWidth",1.0,"DisplayName",nameAverage1);
end
[double(plotData_X(i))+[-0.4 0.4]] gives me the number of the X position and gives the range for x [24.6000 25.4000]
[plotData_Y_mittel(i) plotData_Y_mittel(i)] gives me the height / avg value of y as [15.0 15.0] or as [NaN NaN]
The problem is the legend. Looping through it like that gives me one legend entry for each iteration.
I tried plotting it outside the loop with
plot([double(plotData_X)+[-0.4 0.4]],[plotData_Y_avg plotData_Y_avg],"Color",colourValue1,"LineWidth",1.0,"DisplayName",nameAverage1);
But that gives me two legend entries and the bars aren't plotted. (Or I can't find them.)
How can I display the bars without all the duplicate legend entries? And without having to create the legend manually.
  2 件のコメント
Daniel Neyers
Daniel Neyers 2024 年 1 月 15 日
For now I helped myself with adding an empty plot before the loop and disabled the HandleVisibility.
plot(nan,nan,"Color",colourValue1,"LineWidth",1.0,"DisplayName",nameAverage1);
for i=1 : 1 : size(plotData_X,1)
plot([double(plotData_X(i))+[-0.4 0.4]],[plotData_Y_avg(i) plotData_Y_avg(i)],"Color",colourValue1,"LineWidth",1.0,"HandleVisibility","off");
end
Looping over plotData_X size is a bit iffy, but works as long as plotData_X, plotData_Y and plotData_Y_avg have the same size.
Cris LaPierre
Cris LaPierre 2024 年 1 月 15 日
Please save your data to a mat file and attach it to your post using the papercilp icon.

サインインしてコメントする。

回答 (1 件)

Avni Agrawal
Avni Agrawal 2024 年 1 月 23 日
Hi Daniel,
I understand that you want to avoid duplicate legend entries for the scatter plot. To achieve this, you can use the DisplayName property only for the first iteration and then use the Annotation property set to 'off' for the remaining iterations. This way, only one entry will appear in the legend. Here's how you can modify your loop:
% Assume plotData_X, plotData_Y_avg, colourValue1, and nameAverage1 are defined
% Start the loop
for i = 1:size(plotData_X,1
if i == 1
% For the first iteration, add the DisplayName for the legend
plot([double(plotData_X(i))+[-0.4 0.4]], [plotData_Y_avg(i) plotData_Y_avg(i)], ...
'Color', colourValue1, 'LineWidth', 1.0, 'DisplayName', nameAverage1);
else
% For subsequent iterations, turn off the annotation so it doesn't appear in the legend
plot([double(plotData_X(i))+[-0.4 0.4]], [plotData_Y_avg(i) plotData_Y_avg(i)], ...
'Color', colourValue1, 'LineWidth', 1.0, 'Annotation', 'off');
end
hold on; % Make sure to add this if you want all lines to be on the same plot
end
hold off; % Release the hold after plotting
% Now you can create the legend and it will only have one entry for the average
legend();

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by