I have a box plot for which I want the label.
the variable on the x-axis in latex
the code works if I put this, however the the labels are not in latex
boxplot(a,'Labels',{'sigma = 10','sigma = 20','sigma = 30'})
to put it into latex I would write the following command
boxplot(a,'Labels',{'sigma = 10','sigma = 20','sigma = 30'} , 'interpreter' , 'latex')
However , this gives me an error
Invalid parameter name: interpreter.
How would I fix it?
Use
boxplot(a,'Labels',{'sigma = 10','sigma = 20','sigma = 30'})
set(gca, 'TickLabelInterpreter', 'latex')
Related
In the code below I try to highlight a specific column by red. However, the resulting label color bar is not aligned with the labels, as shown in the image. How can I correct this?
datdat = randn(5,10);
regregnames = {'A', 'B', 'C', 'D', 'E'};
colors = cell(1,size(datdat,2));
for i=1:size(datdat,2)
colors{i} = [1,1,1];
end
colors{3} = [1,0,0];
s.Labels = arrayfun(#num2str, 1:size(datdat,2), 'UniformOutput', false);
s.Colors = colors;
clscls = clustergram(datdat, 'RowLabels', regregnames, 'ColumnLabels', s.Labels, 'ColumnLabelsColor', s, 'LabelsWithMarkers', true);
This is definitely a bug in MATLAB. How do I know? By inspecting the clustergram.plot function.
If we set a breakpoint on line 142, positionAxes(obj, imAxes), and run your code until that point, we get the following figure:
where the alignment is correct, but the dendrogram is not visible. Then, the code proceeds to reposition the axes (mainly making them smaller), while unfortunately neglecting the bottom part with the red label.
To understand how to fix this, we should go a bit back, into HeatMap.plot > initHMAxes where this bottom bar is created and find where its handle is stored. Then, all we need to do is adjust the position of this element in accordance with the rest of the clustergram (HeatMap).
I'll leave digging through the handles/appdata "as an exercise to the reader", but long story short, just add this to the end of your code:
hAx = struct(clscls).HMAxesHandle;
data = getappdata(hAx, 'HeatMapAxesData');
data.XMarkerAxes.Position = data.XMarkerAxes.Position.*[0 1 0 1] + hAx.Position.*[1 0 1 0];
The result:
BTW, on R2017b I'm getting the following warning:
Warning: COLUMNLABELSCOLOR is not supported and will be removed in a future release.
Use LabelsWithMarkers for similar functionality.
So technically this is not a bug, but rather an unsupported feature.
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.
I am trying to plot a histogram with a legend that consists of two lines.
Running the following code leads to the error:
Error using matlab.graphics.chart.primitive.Histogram/set
Value cell array handle dimension must match handle vector length.
xErr = randn(1,1000);
[mu, sig] = normfit(xErr);
h = histogram(xErr, 100, 'Normalization','pdf');
% The following command causes the error
set(h_xErr, {'DisplayName'}, {['Standard deviation $\sigma_{x} = $ ', num2str(sigX)]; ['Mean $\mu_x = $ ', num2str(muX)]});
hl = legend('Location', 'NorthWest');
set(hl,'Interpreter','latex');
I also tried the DisplayName property directly with the histogram command but this doesn't work either. According to this question it is necessary that the dimension of the cell array also matches the number of handles which the error states too.
I thought of adding another handle with still the same error.
h = [h; histogram(xErr, 100, 'Normalization','pdf')];
Is there a simple way to get two lines in the legend of a histogramm?
I am using Matlab R2016b
Per the DisplayName documentation, a newline character \n needs to be injected into the text, and this can easily be done through sprintf. One small but important complication is that escaping the standard LaTeX active character \ is required, so sprintf doesn't think LaTeX commands are one of its special characters (some variable names were changed to ensure the code runs):
xErr = randn(1,1000);
[mu, sig] = normfit(xErr);
h = histogram(xErr, 100, 'Normalization','pdf');
set(h,...
'DisplayName',...
sprintf([...
'Standard deviation $\\sigma_{x} = $ ', num2str(sig),...
'\nMean $\\mu_x = $ ', num2str(mu)]));
hl = legend('Location', 'NorthWest');
set(hl,'Interpreter','latex');
I would personally use
xErr = randn(1,1000);
[mu, sig] = normfit(xErr);
histogram(xErr, 100, 'Normalization','pdf');
legText = {...
sprintf([...
'Standard deviation $\\sigma_{x} = %9.7f$ \n ',...
'Mean $\\mu_x = %9.7f$' ],...
[sig,mu])...
};
legend(legText,'Location', 'NorthWest','Interpreter','latex');
but that's just aesthetics.
So, as I browse through google on the problem of how to create a .gif animation from series of .fig files, i stumble through one that uses .sdf file, I tried to rewrite the program to work for my .fig files
clear all;
close all;
dynam = 156;
gif_fps = 24;
video_filename = 'A.gif';
fh = figure(1);
for i = 1:dynam
F_data = getdata(['F' num2str(i,'_%03i', '.fig');
imagesc(F_data);
drawnow;
frame = getframe(fh);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if a == 0;
imwrite(imind,cm,video_filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,video_filename,'gif','WriteMode','append','DelayTime',1/gif_fps);
end
end
so it pops up an error saying
??? frame = getframe(fh);
|
Error: The expression to the left of the equals sign is not a valid target for an assignment.
which I don't understand why this is happening, and I also notice that Matlab is not drawing the figs, the figure that pop us is completely blank.
The error comes from a typo. The line
F_data = getdata(['F' num2str(i,'_%03i', '.fig'); %bracket missing at the end
should read
F_data = getdata(['F' num2str(i,'_%03i', '.fig']);
Without the bracket, Matlab sees
['F' num2str(i,'_%03i', '.fig');
imagesc(F_data);
drawnow;
frame
as a single string of letters. The code's logic is therefore a = b = c and matlab can't interpret this.
To prevent such errors, matlab and its editor have some nice coloring schemes that highlight in dark red the text following an opening string ', and turn the full string in purple when a closing ' is used. If you see some red characters spanning over several lines, that's a sign of a potential problem. Unfortunately, brackets don't have such behavior...
Additionnaly, what about opening the figures per se? You will see if each figure renders well (no blank) and will be able to capture the frame.
for i = 1:dynam
%open, get the frame from and close the figure
fh_tmp = open(['F' num2str(i,'_%03i', '.fig'])
frame = getframe(fh_tmp);
close(fh_tmp);
im = frame2im(frame);
...
I still struggle to find where the getdata is coming from.
I have some matlab code I have created for plotting multiple lines in the plot. However it does not seem to plot the text at the end of the line like I was hoping for. Does anyone have an idea of a way to make easier and why it might not be working.
format long;
options = optimset('TolFun',1e-12);
vfb = -0.9;
fT= 0.026;
fF = 0.4591431;
gamma = 0.2377589;
datafile = fopen('quiz3p2bresults.text','w');
if datafile == -1
error('Error opening data file!');
end
fprintf(datafile, '%s\t %s\n', 'Vgb', 'PSIL')
vgb = 1:0.5:2;
vgb = vgb';
vdb = 0:0.1:1.5;
vdb = repmat(vdb,3,1);
eqn = #(psiL) (vgb-vfb-gamma*(sqrt((psiL-vdb)/fT)));
result = fsolve(eqn,vgb,options);
plot(vdb,result(1,:),'r',vdb,result(2,:),'y',vdb,result(3,:),'g');
text(max(vdb), max(result), num2str(vgb));
fclose(datafile);
Try
text(max(vdb), result( : , max(size(result)) ), num2str(vgb));
You might also want to change the plot to
plot(vdb(1,:),result(1,:), ...