Display Glitch: Overlay of plots don't display requested LineStyle
26 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Walter Roberson
2025 年 2 月 13 日 20:31
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
採用された回答
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
ax.LineStyleOrder
Axes properties after calling yyaxis():
yyaxis left
ax.ColorOrder
ax.LineStyleOrder
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 件)
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!