I would like to change the legend style of the following picture generated in Matlab:
x1=-5;
x2=5;
y1=-5;
y2=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
fill(x,y,'b')
legend('A')
As you can see the legend displays a blue rectangle. What I would like is a filled blue circle in place of the rectangle as if the picture was generated as a scatter plot. How can I obtain that?
I would suggest to add a fictive value with hold on; p = plot(NaN, NaN, 'b.', 'MarkerSize', 15); then legend this specific "fake" plot with: legend(p, 'A');
x1=-5;
x2=5;
y1=-5;
y2=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
fill(x,y,'b');
hold on; p = plot(NaN, NaN, 'b.', 'MarkerSize', 15);
legend(p, 'A')
#Bebs has a nice solution.
Another suggest is to change directly the legend icon:
[a,b] = legend('A');
b(2).Xdata = sin(-pi:0.1:pi)/10+0.4; % you can play with numbers to set size and location of circle
b(2).Ydata = cos(-pi:0.1:pi)/5+0.5;
Now you can set some other properties:
b(2).LineWidth = 1; % thicker line
b(2).FaceColor = [1 1 1]; % white fill
b(2).EdgeColor = [0 0 1]; % blue edge
Related
I am now having trouble plotting something by matlab.
x1 = 1:2500;
y1 = 1:2500:2500^2;
y2 = 1:2400:2500*2400;
figure
subplot(2,2,1);
semilogy(x1, y1, '-', x1, y2, '-.');
set(gca,'xticklabel',{[]});
pp = subplot(2,2,2);
pospp = get(pp, 'Position');
ax3 = axes('Position',pospp);
ax4 = axes('Position',[0.7 0.7 0.1 0.1]);
axes(ax3);
semilogy(x1, y1, '-', x1, y2, '-.');
set(gca,'xticklabel',{[]});
axes(ax4);
semilogy(2000:2500, y1(2000:2500), '-', 2000:2500, y2(2000:2500), '-.');
set(ax3,'xticklabel',{[]});
set(ax4,'xticklabel',{[]});
Here is the figure
I am wondering how can I remove one y axis from the right subplot? The one with "0, 0.2, 0.4, 0.6, 0.8, 1.0"? I only want to keep the log-scale one.
Thank you.
In this line:
ax3 = axes('Position',pospp);
you create an extra axes object. Remove this line and the axes will be gone. Use pp instead of ax3.
I am looking for a way to shade the background of my semilog plot in two colors.
For example, in the following image, I have am plotting three polynomials and they all are equal at x=1. I want one rectangle for x<1 region and other for x>1 region. How can I insert two such rectangles, of different colors, in background to highlight these two regions.
MWE:
x = 0.1:0.1:10;
y1 = polyval([1, 0], x); % Evaluate y = x;
y2 = polyval([1, 0, 0], x); % Evaluate y = x^2;
y3 = polyval([1, 0, 0, 0], x); % Evaluate y = x^3;
figure
semilogy(x, y1, '.k', x, y2, '.b', x, y3, '.r'); title ('Three
polynomials on a semilog y scale') xlabel('x'); ylabel('y');
legend({'y= x', 'y = x^2', 'y = x^3'}, 'Location', 'Northwest')
You can solve that using area or patch.
As pointed by #SardarUsama, there are others questions with good examples on it, however, you need to avoid to have any zeros in the area data, otherwise it will fail.
Follows the code setting one area only.
x = 0.1:0.1:10;
y1 = polyval([1, 0], x); % Evaluate y = x;
y2 = polyval([1, 0, 0], x); % Evaluate y = x^2;
y3 = polyval([1, 0, 0, 0], x); % Evaluate y = x^3;
figure
plot(x, y1, '.k', x, y2, '.b', x, y3, '.r'); %MODIFIED
hold on %ADDED
title ('Three polynomials on a semilog y scale')
set (gca, 'Yscale', 'log'); %ADDED
xlabel('x');
ylabel('y');
legend({'y= x', 'y = x^2', 'y = x^3'}, 'Location', 'Northwest')
area( [1 1 10 10],[1e-3 1e+3 1e+3 1e-3 ],'FaceColor','green','facealpha',0.3) %ADDED
The code above works for matlab after 2014b. If you have one before that, you can use the patch function (which requires some small change in the data, but uses the Facealpha option) or you can move the area to the background as i do below:
ax=get(gca,'Children'); %ADDED
set(gca,'Children',[ax(2) ax(3) ax(4) ax(1)]); %ADDED, move area to background
Note: Indeed, I missed the problem with the legend. I correct as mentioned, however for me the area was on top of the other graphs. To solve it i changed the order of the plots. If the area was with transparency, this will not be an issue.
I am having trouble getting my title to show when I have a figure with two x-axis.
The plot looks good and the axis scales are as I would like them to be but the second axis label and the title end up outside my figure.
How do I get the plot and axis to have the same size and change the size of the figure to include labels and title?
Here is a minimal example:
x1 = linspace(0, 5);
y11 = sin(x1);
y12 = cos(x1);
x2 = linspace(4, 12);
figure(1)
plot(x1, y11, 'r');
hold on
grid on
plot(x1, y12, 'k');
axis([0 5 -1 1.8]);
legend('sin(x)', 'cos(x)');
xlabel('x')
ylabel('y-label');
ax1 = gca;
ax1_pos = ax1.Position;
ax2 = axes('Position', ax1_pos,...
'XAxisLocation', 'top',...
'YAxisLocation', 'right',...
'Color', 'none');
ax2.YColor = 'w';
title('2:nd Harmonics');
line(x2,0,'Parent',ax2,'Color','k')
xlabel('n');
As a workaround you could pre-define the Position property (i.e. size) of the 1st axes before generating the plot so that the title appears correctly even if you add a 2nd axes. For example, right after the call to figure(1) add something like this:
ax1 = axes('Position',[0.11 0.11 0.75 0.75]);
Also, if you wish to print exponent values in the title you can use Latex formatting as follows:
title('2^{nd} Harmonics');
Here is the whole code with output:
clear
clc
close all
x1 = linspace(0, 5);
y11 = sin(x1);
y12 = cos(x1);
x2 = linspace(4, 12);
figure(1)
%// Set axes position manually
ax1 = axes('Position',[0.11 0.11 0.75 0.75]);
plot(x1, y11, 'r');
hold on
grid on
plot(x1, y12, 'k');
axis([0 5 -1 1.8]);
legend('sin(x)', 'cos(x)');
xlabel('x')
ylabel('y-label');
%ax1 = gca;
ax1_pos = get(ax1,'Position');
ax2 = axes('Position', ax1_pos,...
'XAxisLocation', 'top',...
'YAxisLocation', 'right',...
'Color', 'none');
set(ax2,'YColor','w');
%// Notice the Latex formatting to print the exponent
title('2^{nd} Harmonics');
line(x2,0,'Parent',ax2,'Color','k')
xlabel('n');
Then you can resize as you wish; the title stays visible.
I am trying to program scatterplot with specific errorbars. The only build in function i found is
errorbar()
but this only enables me to make a 2d plot with errorbars in y direction. What i am asking for is a method to plot this with errorbars in x and y direction.
At the end my goal is to make a 3D-scatter-plot with 3 errorbars.
Perfect would be if the resulting image would be a 3d-plot with 3d geometric shapes (coordinate x,y,z with expansion in the dimension proportional to the errorbars) as 'marker'.
I found this page while searching the internet: http://code.izzid.com/2007/08/19/How-to-make-a-3D-plot-with-errorbars-in-matlab.html
But unfortunately they use only one errorbar.
My data is set of 6 arrays each containing either the x,y or z coordinate or the specific standard derivation i want to show as errorbar.
The code you posted looks very easy to adapt to draw all three error bars. Try this (note that I adapted it also so that you can change the shape and colour etc of the plots as you normally would by using varargin, e.g. you can call plot3d_errorbars(...., '.r'):
function [h]=plot3d_errorbars(x, y, z, ex, ey, ez, varargin)
% create the standard 3d scatterplot
hold off;
h=plot3(x, y, z, varargin{:});
% looks better with large points
set(h, 'MarkerSize', 25);
hold on
% now draw the vertical errorbar for each point
for i=1:length(x)
xV = [x(i); x(i)];
yV = [y(i); y(i)];
zV = [z(i); z(i)];
xMin = x(i) + ex(i);
xMax = x(i) - ex(i);
yMin = y(i) + ey(i);
yMax = y(i) - ey(i);
zMin = z(i) + ez(i);
zMax = z(i) - ez(i);
xB = [xMin, xMax];
yB = [yMin, yMax];
zB = [zMin, zMax];
% draw error bars
h=plot3(xV, yV, zB, '-k');
set(h, 'LineWidth', 2);
h=plot3(xB, yV, zV, '-k');
set(h, 'LineWidth', 2);
h=plot3(xV, yB, zV, '-k');
set(h, 'LineWidth', 2);
end
Example of use:
x = [1, 2];
y = [1, 2];
z = [1, 2];
ex = [0.1, 0.1];
ey = [0.1, 0.5];
ez = [0.1, 0.3];
plot3d_errorbars(x, y, z, ex, ey, ez, 'or')
I am plotting two curves in different axes in the same figure with plotyy. The first curve ranges from 10^-4 to 10^-1 and the second curve ranges from 0 to 10. If I plot in the following way,
[AX, H1, H2] = plotyy(x, y1, x, y2, 'semilogy', 'semilogy');
They will both plotted as semilogy and with correct scale in y. But I don't want to show y2 in log10 scale, so I change
[AX, H1, H2] = plotyy(x, y1, x, y2, 'semilogy', 'plot');
However, then on left and right y axis, the tick only show the min and max range, all detail inbetween gone. Why's that?
You can try this:
[AX, H1, H2] = plotyy(x, y1, x, y2, 'semilogy', 'plot');
% set yticks for the left axis
set(AX(1), 'ytick', yourDesiredYticks1)
set(AX(1), 'box', 'off') % to remove corresponding yticks on the right side of the plot
% set yticks for the right axis
set(AX(2), 'ytick', yourDesiredYticks2)
set(AX(2), 'box', 'off')
Try this:
%# create some data resembling what you described
x = 1:100;
y1 = rand(size(x))*1e-1 + 1e-4;
y2 = rand(size(x))*10;
%# plot
hAx = plotyy(x,y1, x,y2, 'semilogy', 'semilogy');
set(hAx(2), 'YScale','linear')