Display Glitch: Overlay of plots don't display requested LineStyle
Show older comments
Hello all,
I'm displaying some data in a particular way and am running into a very strange display glitch. The requested 'LineStyle' property using the plot function is being overwritten/ignored when called multiple times in a for loop. I have prepared a script that reproduces the issue. Please change the "iterationNum" variable to see the issue persist. I'd appreciate intuition on the origin of this issue or work-arounds people find, ideally without resorting to a case-by-case post-correction.
I am using MATLAB version 2023a.
% The bug: lines stop plotting as requested on certain iterations
% MATLAB Version R2023a
% Variable to change: Try 2, 3, 5, 8
iterationNum = 8;
% Less important variable: used for displaying
spacerVal = 10;
alphaVal = 0.2;
xData = 0:25;
testMeans1 = spacerVal/2*rand(size(xData));
testSTDs1 = spacerVal/2*rand(size(xData));
testMeans2 = spacerVal/2*rand(size(xData));
testSTDs2 = spacerVal/2*rand(size(xData));
% Display lines for shifting
shiftBases = ((1:iterationNum)-1)*spacerVal;
[xBG,yBG] = ndgrid([xData(1) xData(end)],shiftBases);
figure
plot(xBG,yBG,'black');
hold on
% Loop over iterations and plot; use breakpoints or ginput() if you want to
% pause
for index = 1:iterationNum
currColor = [index/iterationNum 0 (1-index/iterationNum)];
yyaxis 'left'
plotMeanStdPoly(testMeans1+shiftBases(index),testSTDs1,xData,currColor,alphaVal);
yyaxis 'right'
plotMeanStdPoly(testMeans2+shiftBases(index),testSTDs2,xData,currColor,alphaVal);
end
function [] = plotMeanStdPoly(meanVals,stdVals,xVals,currColor,alphaVal)
%PLOTMEANSTDPOLY Sloppy bundle function to plot meanVals with a bounding
%polygon of +/- stdVals.
% Generate a polynomial for background standard deviation
[stdPolyX,stdPolyY] = getPolyCoords(meanVals,stdVals,xVals);
nonanLogic = ~isnan(stdPolyY);
currPoly = polyshape(stdPolyX(nonanLogic),stdPolyY(nonanLogic));
% Plot
polyPlot = plot(currPoly);
hold on
polyPlot.FaceAlpha = alphaVal;
polyPlot.EdgeColor = 'none';
polyPlot.FaceColor = currColor;
plot(xVals,meanVals,'Color',currColor,'LineWidth',2,'LineStyle','--')
end
function [outXs,outYs] = getPolyCoords(yCoords,yHeights,xCoords)
%GETPOLYCOORDS Takes in a set of y-coordinates, y-heights, and optional
% x-coordinates. Returns a set of coordinates that coorespond to a
% polynomial whose vertices are y-coordinates +/- y-heights at each
% corresponding x-coordinate. Used for plotting standard deviations over
% means.
% Define xCoords, if not passed
if nargin == 2
xCoords = 1:numel(yCoords);
end
% Generate x-coordinates by reflecting
outXs = [xCoords(:); flipud(xCoords(:))]';
% y-coordinates go +height one way and -height the other way
outYsPlus = yCoords + yHeights;
outYsMinus = yCoords - yHeights;
outYs = [outYsPlus, fliplr(outYsMinus)];
end
Thanks!
2 Comments
Walter Roberson
on 13 Feb 2025
The only getPolyCoords I can find is some python code at https://stackoverflow.com/questions/55659835/trying-to-separate-polygon-data-into-x-and-y-coordinates-but-get-error-multip
Remi Boros
on 13 Feb 2025
Accepted Answer
More Answers (1)
Remi Boros
on 13 Feb 2025
0 votes
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
