Display Glitch: Overlay of plots don't display requested LineStyle

26 ビュー (過去 30 日間)
Remi Boros
Remi Boros 2025 年 2 月 13 日 20:05
コメント済み: Remi Boros 2025 年 2 月 14 日 0:22
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 件のコメント
Remi Boros
Remi Boros 2025 年 2 月 13 日 21:26
Hi Walter,
Thanks for the quick response. I've updated my post to include the relevant function.
Thanks!

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

採用された回答

Voss
Voss 2025 年 2 月 13 日 21:29
編集済み: Voss 2025 年 2 月 13 日 21:30
Looks like the problem is that yyaxis() changes certain axes properties, including ColorOrder and LineStyleOrder:
figure()
ax = gca();
Axes properties before calling yyaxis():
ax.ColorOrder
ans = 7×3
0 0.4470 0.7410 0.8500 0.3250 0.0980 0.9290 0.6940 0.1250 0.4940 0.1840 0.5560 0.4660 0.6740 0.1880 0.3010 0.7450 0.9330 0.6350 0.0780 0.1840
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ax.LineStyleOrder
ans = '-'
Axes properties after calling yyaxis():
yyaxis left
ax.ColorOrder
ans = 1×3
0 0.4471 0.7412
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ax.LineStyleOrder
ans = 7x2 char array
'- ' '--' ': ' '-.' 'o-' '^-' '*-'
Then plot() cycles through LineStyleOrder each time it is called, apparently respecting the LineStyle from the line spec you used in the plot() call (which is always '--'), but evidently also using the marker from the axes LineStyleOrder. E.g., in this case the first four rows of ax.LineStyleOrder are specifications without a marker, then rows 5-7 have markers, then the cycle repeats, and those are the markers you see in the plotted lines.
N = 10;
x = 0:9;
y = 0:9;
hold on
for ii = 1:N
y = y+1;
plot(x,y,'LineStyle','--')
end
  1 件のコメント
Remi Boros
Remi Boros 2025 年 2 月 14 日 0:22
Thank you for the thorough investigation. This is good to know for the future!

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

その他の回答 (1 件)

Remi Boros
Remi Boros 2025 年 2 月 13 日 20:11
Hello all,
I realize that the problem I'm running into is related to the 'Marker' property, not 'LineStyle.' By setting 'Marker' to 'none' the problem is solved. Still: I would appreciate any intuition as to why MATLAB chooses to display Markers in some instances and not others.
Thank you all!

カテゴリ

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

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by