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:
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:
And so forth. plotData_Y_avg contains average values for each point in X:
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.