Why does Matlab add this line to plot when saving? - matlab

I've been trying to plot some inequalities in Matlab.
When it shows in Matlab figure, it looks right:
But when I save the figure, I get this annoying yellow line (both when saved manually and when saved in code):
The code which produces the plot is:
function [ ] = plotInequalities( ~ )
pRange = linspace(1/2,1,1000);
cRange = linspace(0,1,1000);
[P, C] = meshgrid(pRange,cRange);
ineq1 = P >= 2/3;
ineq2 = C.*P.*(3-4.*P)./(2.*P+C.*(2-4.*P)) >= 1-P;
ineq3 = C <= 3.*P.*(1-P)./(2.*(-6.*P.^2+6.*P-1));
rest = ~ineq1 & ~ineq2 & ~ineq3;
pl = figure
hold on
c = 2:5;
contourf(pRange, cRange, c(2) * ineq2, [c(2), c(2)], 'c')
contourf(pRange, cRange, c(3) * ineq3, [c(3), c(3)], 'y')
contourf(pRange, cRange, c(4) * rest, [c(4), c(4)], 'r')
contourf(pRange, cRange, c(1) * ineq1, [c(1), c(1)], 'b')
legend('\{A,AB\}', '\{A,B\}', '\{A,AB, B\}', '\{A\}')
xlabel('P')
ylabel('C')
saveas(pl, 'out.png','png');
end
I'm using Matlab R2014a on Windows 8.
Any idea as to why this happens?

This is because there is an overlap between your domain ineq1 and ineq3.
If you set the renderer of the figure to anything else than painter (e.g. opengl or zbuffer) you will see the line which represent the border of your domain ineq3 (which should be hidden under ineq1)
When the figure is printed with the Matlab engine (for png,jpg,tiff etc ...), I couldn't force the print command to use the painter renderer. If you use one of the format rendered with the gostscript engine (pdf, bmp,pcx,pcm,...) then the proper output is produced.
If you want to stick to png output, the simple way around is to ensure there is no overlap between your domains before you send them to the contourf function. So in your case just add the line:
ineq3(ineq3==ineq1) = false ;
just before you call the different contourf, and the ouput will be OK in the figure and in the image saved (because there will be no ghost line to confuse the rendering engine).
Of course with this method, the order of overlap is important. This solution assume you want to see the full domain ineq1 and it has priority over the domain ineq3. If you want a different priority you have to change which domain override the other.
PS: and if you want the border of all domains to be visible, consider using patches and transparency so the overlaps will be more obvious.

Related

How to cut out parts of the y-axis for certain x-values in Matlab plots

Is there a (manual or automatic) way of cutting out several parts of the y-axis for certain x-values in a Matlab plot? I found solutions in other programming languages (see 2 links below), but not for Matlab, except for BreakAxis and BreakYaxis on File Exchange, but this only works for one break.
I am also posting my code below, for which I would like to implement it. I would like to have a y-axis break for each yNegData and yPosData, i.e. two breaks, each at [0.3*min(yNegData) 0.7*min(yNegData] and [0.3*max(yPosData) 0.7*max(yPosData].
If you could write it in a way that I could use it for different kinds of plots (not only bar, but also line, for example), that would be very useful.
http://lagrange.univ-lyon1.fr/docs/matplotlib/examples/pylab_examples/broken_axis.html
Using gnuplot, how to 'cut out' usused y-axis regions of the graph
The code:
revenue = ones(100,1);
opex = -1*ones(100,1);
opex(10:15,1) = 3;
data{1} = revenue;
data{2} = opex;
colors = parula(numel(data));
labels = {'Revenue','Opex'};
for i = 1:numel(data)
dataNeg{i} = data{i};
dataNeg{i}(data{i}>0) = 0;
dataPos{i} = data{i};
dataPos{i}(data{i}<0) = 0;
mdata(i) = nnz(dataPos{i}); % was: mean(data{i});
end
[~,posOrder] = sort(mdata,'ascend');
[~,negOrder] = sort(mdata,'descend');
yDataPos = [dataPos{posOrder}];
yDataNeg = [dataNeg{negOrder}];
hold on;
bNeg = bar(yDataNeg,'stack');
bPos = bar(yDataPos,'stack');
for i= 1:numel(data)
set(bNeg(i),'FaceColor',colors(negOrder(i),:))
set(bPos(i),'FaceColor',colors(posOrder(i),:))
end
legend(labels{:});
hold off;
There is this package
https://nl.mathworks.com/matlabcentral/fileexchange/45760-break-y-axis
which works for lines; it has a little example
a=20*rand(21,1)+10;
figure;hold on;
plot(a);
breakyaxis([14 21]);
% hold off %% I guess, inserted by me
However, it does not seem to work as well for bar plots -- if you replace plot(a) by bar(a) in the example above the split stripe does not cover the width of the axes. It could be tweaked I'm sure but that might not be worth the effort.
If you're OK with switching to R you can use gap.barplot() from the plotrix package (see this example) and you can also still switch to gnuplot (see this example).

Title over a group of subplots

I want a figure with six plots inside; I split it with subplots. For example
for i = 1:12
subplot(3,4,i)
plot(peaks)
title(['Title plot ',num2str(i)])
end
I would like to add two global titles, let's say a global title for the six plots on the left hand side and another title for the six other plots on the right hand side.
I don't have 2018b version, so I cannot use sgtitle('Subplot Title');. Is it possible use suptitle('my title'); somehow?
I can use text() but resizing the window, the two labels move.
You can use annotation for that, with the location of subplots 1 and 3:
for k = 1:12
sp(k) = subplot(3,4,k);
plot(peaks)
title(['Title plot ',num2str(k)])
end
spPos = cat(1,sp([1 3]).Position);
titleSettings = {'HorizontalAlignment','center','EdgeColor','none','FontSize',18};
annotation('textbox','Position',[spPos(1,1:2) 0.3 0.3],'String','Left title',titleSettings{:})
annotation('textbox','Position',[spPos(2,1:2) 0.3 0.3],'String','Right title',titleSettings{:})
I did not test this, but you can get the handle to a subplot object and then perform the title method on this handle. I would also suggest to then apply the title after the loop.
CODE
for k = 1:12
h(k) = subplot(3, 4, i)
plot(peak)
end
title(h(1), 'Left side')
title(h(8), 'Right side') % find out the right index yourself
Remark:
Do not use i or j as iteration variable for they are already defined in the namespace of MATLAB as imaginary unit.

How to plot a "moving" graph for real time values along the x-axis (using psychtoolbox)?

I am writing a code for a real time experiment using psychtoolbox to present the stimulus. In my experiment, I need to show the subject a graph that indicates his performance. I have plotted the graph using this simple code:
% Draw the graph
figure('visible','off','color',[0 0 0]);
pcolor([0 Num_timepoint+2],[-10 0],ones(2,2));
hold on;
pcolor([0 Num_timepoint+2],[0 10],2*ones(2,2));
colormap([79 167 255;255 187 221]/256);
plot(1:subloop,value0,'*-',...
'color',[0,0,0],...
'LineWidth',1,...
'MarkerSize',5,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0.5,0.5,0.5]);
axis([0,Num_timepoint+2,-10,10]);
saveas(gcf,'line_chart.png'); %save it
close(figure);
line_chart=imread('line_chart.png'); %read it
resized_plot=imresize(line_chart,0.5);
imageSize = size(resized_plot);
[imageHeight,imageWidth,colorChannels]=size(resized_plot);
bottomRect = [xCenter-imageWidth/1.5, yCenter+gapdown, xCenter+imageWidth/1.5, yCenter+gapdown+imageHeight];
imageDisplay=Screen('MakeTexture', win0, resized_plot);
Screen('DrawTexture', win0, imageDisplay, [], bottomRect);
Unfortunately, this simple code is very slow. In addition, I couldn't make the graph moving along x axis, as soon as the new value comes.
Any help would be Awesome. Thanks in advance for your efforts.
Why are you saving the figure and redisplaying as an image? Maybe I'm missing something but you should be able to accomplish what you need by updating the existing plot with the fresh data using the handles properties of the plot:
.... First time through we need the initial plot ....
% Set up figure with colormaps and such but leave as 'visible','on'
hPlot = plot(plot(1:subloop,value0,'*-',...
'color',[0,0,0],...
'LineWidth',1,...
'MarkerSize',5,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0.5,0.5,0.5]);
hAxes = gca;
.... Loop over your real time updates ....
% Assuming value0 has subject's results from t=0 to t=now
hPlot.XData = value0; hPlot.YData = [1:subloop];
hAxes.XLim = [0 numTimePoints+2];
hAxes.YLim = [-10 10]
.... Continue test and update value0 ....
I think that should keep your plots current without having to save the figure as image to file then reopen the image to display to subject.
If you want to move your data one sample, you can use the circshift function. For example, if you want your new values to appear on the left hand side, you can shift all values 1 sample rightward, then add your new value in the first position.
For converting a MATLAB figure to a Psychtoolbox texture, you don't need to save, then load the temporary images. You can instead use the getframe function to capture the MATLAB figure data, which can then be given to MakeTexture to turn it into a Psychtoolbox texture.
I'm not sure what values you're actually using for subloop, value0, etc. but there is an example that I think might be close to what you want. In this example, 30 frames of figures are plotted, with each figure being on screen for 1 second. New data points are generated randomly and appear from the left hand side of the figure.
Depending on the details of your experiment, you may find that this approach is still too slow. You could also create the figure directly via Psychtoolbox drawing methods like DrawLines, etc. though that would require more effort.
try
win0 = Screen('OpenWindow', 0, 0);
Num_timepoint = 100;
subloop = 100;
value0 = zeros(1,100);
num_demo_frames = 30;
% Draw the graph
fig_h = figure('visible','off','color',[0 0 0]);
pcolor([0 Num_timepoint+2],[-10 0],ones(2,2));
hold on;
pcolor([0 Num_timepoint+2],[0 10],2*ones(2,2));
colormap([79 167 255;255 187 221]/256);
plot_h = plot(1:subloop,value0,'*-',...
'color',[0,0,0],...
'LineWidth',1,...
'MarkerSize',5,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0.5,0.5,0.5]);
axis([0,Num_timepoint+2,-10,10]);
for f = 1:num_demo_frames
new_value = randn(1,1);
data_values = plot_h.YData;
data_values = circshift(data_values, 1);
data_values(1) = new_value;
plot_h.YData = data_values;
plot_values = getframe(fig_h);
imageDisplay=Screen('MakeTexture', win0, plot_values.cdata);
Screen('DrawTexture', win0, imageDisplay);
Screen('Flip', win0);
WaitSecs(1);
end
sca;
catch e
sca;
rethrow(e);
end

Why is Matlab placing text spaces in legend using Latex when the figure gets saved as a PDF?

I am trying to put a legend in Matlab figures that include a symbol in Latex. When I plot the figure, the legend looks fine. However, when I export the figure as a PDF, the legend gets spaces put into it. I don't know why this is happening. Sample code is as follows:
set(groot,'defaultLineLineWidth',2,'defaultAxesFontSize',...
12,'defaultAxesFontName','timesnewroman',...
'defaulttextinterpreter','latex')
x0 = 8;
y0 = 5;
width = 5;
height = 4;
kappa1 = 0.1;
kappa2 = 0.5;
f = linspace(0,2*pi,1000);
y1 = sin(f+kappa1*f.^2);
y2 = sin(f+kappa2*f.^2);
figure(1)
hold on
plot(f,y1,'k')
plot(f,y2,'b')
xlabel('Frequency (MHz)')
ylabel('Amplitude')
legend(strcat('\kappa = 0.1 MHz/','\mu','s'),...
strcat('\kappa = 0.5 MHz/','\mu','s'))
grid on;
set(gcf,'units','inches','Position',[x0,y0,width,height],...
'PaperPositionMode','Auto','PaperUnits','Inches',...
'PaperSize',[width, height]);
saveas(gcf,'legendtest.pdf')
It seems like the error happens when I save the file as a PDF. It saves as a JPG just fine. Below are the two images I get. The jpg is:
But the PDF I get is:
I am using Matlab version R2017a on a Mac running OS 10.12.5. Thanks in advance for any help!
This is a known bug. See the bug report according to which it affects the versions from R2014b to R2017a. A workaround is suggested in that bug report as well which is to generate the pdf file by setting the Renderer to opengl i.e.
set(gcf,'Renderer','opengl');
But then the generated pdf file contains a pixel graphic instead of a vector graphic.
Better thing would be to use the respective unicode values which will produce a vector graphic. i.e.
legend([char(954), ' = 0.1 MHz/',char(956),'s'],...
[char(954), ' = 15 MHz/',char(956),'s']); %char(954) equals 'κ', char(956) equals 'μ'
If you want to use italic characters, it is also possible with unicode characters.
legend([char([55349,57093]), ' = 0.1 MHz/',char([55349,57095]),'s'],...
[char([55349,57093]), ' = 15 MHz/',char([55349,57095]),'s']);
%char([55349,57093]) equals '𝜅' (italic κ), char([55349,57095]) equals '𝜇' (italic μ)
Another workaround is to just interpret the whole legend text with Latex:
h = legend(strcat('$\kappa$ = 0.1 MHz/','$\mu$','s'),...
strcat('$\kappa$ = 0.5 MHz/','$\mu$','s'))
set(h,'Interpreter','latex')
It requires some basic Latex knowledge, e.g. you have to wrap all math signs (kappa, mu) with $ and beware if you want to use any special non-english characters. Changes the look of the legend a bit, but arguably for the better.
Btw, you can skip strcat, it does not serve any purpose.
h = legend('$\kappa$ = 0.1 MHz/$\mu$s',...
'$\kappa$ = 0.5 MHz/$\mu$s')
set(h,'Interpreter','latex')
Works just as well, the same goes for the non latex version.

Matlab get vector of specific pixels

I am pretty new to Matlab and encountered a problem when working with images.
I want to get a pixel that is in a specific colour (blue) in the following image:
image
My current code looks something like this:
function p = mark(image)
%// display image I in figure
imshow(image);
%// first detect all blue values higher 60
high_blue = find(image(:,:,3)>60);
%cross elements is needed as an array later on, have to initialize it with 0
cross_elements = 0;
%// in this iteration the marked values are reduced to the ones
%where the statement R+G < B+70 applies
for i = 1:length(high_blue)
%// my image has the size 1024*768, so to access the red/green/blue values
%// i have to call the i-th, i+1024*768-th or i+1024*768*2-th position of the "array"
if ((image(high_blue(i))+image(high_blue(i)+768*1024))<...
image(high_blue(i)+2*768*1024)+70)
%add it to the array
cross_elements(end+1) = high_blue(i);
end
end
%// delete the zero element, it was only needed as a filler
cross_elements = cross_elements(cross_elements~=0);
high_vector = zeros(length(cross_elements),2);
for i = 1:length(cross_elements)
high_vector(i,1) = ceil(cross_elements(i)/768);
high_vector(i,2) = mod(cross_elements(i), 768);
end
black = zeros(768 ,1024);
for i = 1:length(high_vector)
black(high_vector(i,2), high_vector(i,1)) = 1;
end
cc = bwconncomp(black);
a = regionprops(cc, 'Centroid');
p = cat(1, a.Centroid);
%// considering the detection of the crosses:
%// RGB with B>100, R+G < 100 for B<150
%// consider detection in HSV?
%// close the figure
%// find(I(:,:,3)>150)
close;
end
but it is not optimized for Matlab, obviously.
So i was wondering if there was a way to search for pixels with specific values,
where the blue value is larger than 60 (not hard with the find command,
but at the same time the values in the red and green area not too high.
Is there a command I am missing?
Since English isn't my native language, it might even help if you gave me some suitable keywords for googling ;)
Thanks in advance
Based on your question at the end of the code, you could get what you want in a single line:
NewImage = OldImage(:,:,1) < SomeValue & OldImage(:,:,2) < SomeValue & OldImage(:,:,3) > 60;
imshow(NewImage);
for example, where as you see you provide a restriction for each channel using logical operators, that you can customize of course (eg. using | as logical OR). Is this what you are looking for? According to your code you seem to be looking for specific regions in the image like crosses or coins is that the case? Please provide more details if the code I gave you is completely off the track :)
Simple example:
A = imread('peppers.png');
B = A(:,:,3)>60 & A(:,:,2)<150 & A(:,:,1) < 100;
figure;
subplot(1,2,1);
imshow(A);
subplot(1,2,2)
imshow(B);
Giving this: