Im triying to create a semilogy plot where I can see the labels of the minor ticks. The data I'm plotting spans from 292 to 1138. So by default the semilogy plot only shows the label in the 1000. I would need to show also the labels in the minor ticks (with a smaller font would be awesome) since only one number in the entire y axis is not very informative. Thanks a lot!
It seems like there's a bug on Matlab, which makes it not possible to label minor ticks. (thanks #excasa)
One thing you could do is setting the gca's YTick property as a vector of increasing values. Then, there would be labels where you want (even though they would not be minor ticks anymore, but normal ticks).
Example:
ax = gca; %get current axis
ax.XTick = [-3*pi -2*pi -pi 0 pi 2*pi 3*pi];
ax.YTick = [-1 -0.5 0 0.5 1];
% For R2014a and earlier:
% ax = gca;
% set(ax,'XTick',[-3*pi -2*pi -pi 0 pi 2*pi 3*pi])
% set(ax,'YTick',[-1 -0.5 0 0.5 1])
You could, also, activate the grid for minor ticks set(gca,'YminorGrid','on'), so you have a better view of the values (even they not being labeled).
Related
Here is a simple MATLAB code. How to control the default distance between ticks of the Y axis? I want to make it smaller to fit in my paper. Hint: I update the post with 2 picture that shows what I mean (they are the same but the distance between the y axis ticks is smaller in one picture that the other.
x = linspace(-10,10,200);
y = sin(4*x)./exp(x);
plot(x,y)
xlim([0 10])
ylim([-0.4 0.8])
You can control the tick by using the gca object of the plot. Here is an example for xtick. Change 'xtick' to 'ytick':
plot(x,y);
set(gca, 'xtick', [-10:2:10]);
If you want to change the x-axis tick labels with new labels, then you can change the values of the labels as follows:
% specify the ticks first where you want to change
xticks([0 2 4 6 8])
% change the corresponding labels to the required ones
xticklabels({'-1', '-2', '-3', '-4', '-5'})
You can modify the height of the graph, maintaining the number and values of the tick marks, which makes the distance between tick marks smaller.
To do so, set the figure window’s 'Position' property (this is equivalent to dragging the edges of the window to make the figure smaller), and setting the locations of the tick marks manually to prevent MATLAB from reducing their number. For example:
h = gcf; % figure handle
a = gca; % axes handle
ticks = get(a,'YTick');
pos = get(h,'Position');
pos(4) = pos(4) * 0.75; # reduce the size
set(h,'Position',pos);
set(a,'YTick',ticks)
You should also note the PaperPosition, PaperSize and other Paper... properties of the figure, as they are used when printing (also to file). You might want to manually set those properties before creating a PDF or EPS from the graph.
Here is even a simpler way then what #Cris suggested:
ax = axes;
ax.YTickMode = 'manual';
ax.Position(4) = ax.Position(4)*0.75;
by setting the YTickMode to manual you prevent Matlab from updating the ticks upon resizing of the axes. Then you change the hight of the axes by setting the position property directly.
I have a simple plot of y against x.
y = [6,-1.3,-8,-11.7,-11,-6,1.3,8,11.7,11,6,-1.3];
x = 0:0.3:3.3;
plot (x,y)
As the result, the x-axis of the figure is ranging from 0 to 3.5, with scale of 0.5. I have used the XLimit = [0 3.3] to limit the axis, but it seems like not working.
I wish to make the x-axis range from 0 to 3.3 with steps of 0.3.
axis tight % removes the empty space after 3.3
set(gca,'XTick',0:0.3:3.3) % sets the x axis ticks
With XLimit = [0 3.3] you just define a vector called XLimit. In order to use this vector as horizontal limit, you should use xlim:
xlim(XLimit)
% or directly:
xlim([0, 3.3])
Read more about xlim here. Similarly you can set the vertical limit with ylim.
Since you are trying to set the limits equal to the range of x, you will probably find the following command most helpful:
axis tight
But note that it changes both x- and y-axis limits.
To set the tick step, as AVK said, you should set the 'XTick' to 0:0.3:3.3:
set(gca,'XTick',0:0.3:3.3)
gca is the handle to current axes.
I searched and watched how to plot the 3 dimensions of nonlp program's
but I still don't know how to plot these constraints.
x^2+y^2+z^2=1
x>=2*y
2*y>=3*z
x>=3*z
I programmed like this but it doesn't work and I think this is wrong program for upper constraints.
func1 = #(x,y,z) sqrt(1-x.^2-y.^2);
func2 = #(x,y,z) max(x-2*y,0);
func3 = #(x,z) max(x-3*z,0);
ezsurf(func1,[0 1 0 1]);
hold on;
ezsurf(func2,[0 1 0 1]);
hold on;
ezsurf(func3,[0 1 0 1]);
axis([0 1 0 1 0 1]);
A way of doing that is to work with volumetric data.
The idea is to create a 3D space, with a F value. F will be positive in one side of your main equation and negative in the other.
[X,Y,Z]=meshgrid(-2:0.05:2,-2:0.05:2,-2:0.05:2);
F=X.^2+Y.^2+Z.^2-1;
Then the conditions are applied to this F. If you want to see the object "cut"
then substitute the place where conditions are not met with nan. If you want to see a filled object, then substitute it with a positive number (it may need to be negative in another situation, so check this for other examples.
% Conditions
cond1=X>=2*Y;
cond2=2*Y>=3*Z;
cond3=X>=3*Z;
% If you want the boundaries to show
F1=F;
F1(~cond1)=1;
F1(~cond2)=1;
F1(~cond3)=1;
% If you want the boundaries not to show
F2=F;
F2(~cond1)=NaN;
F2(~cond2)=NaN;
F2(~cond3)=NaN;
Then the idea is to create an isosurface in the zero level (your original function). Here in this code I created 2 plots so you can see the differences between filling and not filling the boundaries. Also some fancy coding in order to get nice figures. However the isosurface and patch parts are relevant to obtain the surface and plot it.
subplot(121)
iso1=isosurface(X,Y,Z,F1,0);
p=patch(iso1);
isonormals(X,Y,Z,F1,p);
set(p,'FaceColor','red','EdgeColor','none');
daspect([1 1 1])
axis equal
camlight
lighting gouraud
subplot(122)
iso2=isosurface(X,Y,Z,F2,0);
p=patch(iso2);
isonormals(X,Y,Z,F2,p);
set(p,'FaceColor','red','EdgeColor','none');
axis equal
daspect([1 1 1])
camlight headlight
lighting gouraud
Result:
Additionally, You can calculate the isosurface of the initial boundary F and plot it with a low alpha value, so you can better understand what piece of the domain you are actually selecting.
I am working on matlab programming, my problem is that in the same graph on y axis i need to have variable scaling, for example from 0.1 to 1 i need to have a gap between scales 0.1, but after 1 I need to have scale gap of 2, is there some command available for the same?
There is an example by The Mathworks on Matlab answers which does pretty much what you want to achieve. The idea is to create 2 axes on the same figure and use one axes to plot some data (eg. for the 0.1:0.1:1 tick marks) and the rest on the other axes. Then you overlay both axes with a transparent background:
%Create two overlapping axes
axes_handle_1 = axes;
axes_position = get(axes_handle_1, 'Position');
axes_handle_2 = axes('Position', axes_position);
%Create some data with a large gap in the x domain
my_x_data = [1:10 25:35];
my_y_data = rand(1, length(my_x_data));
%Plot the two sections of data on different axes objects
plot(axes_handle_1, my_x_data(1:10), my_y_data(1:10))
plot(axes_handle_2, my_x_data(11:end), my_y_data(11:end))
%Link the y axis limits and fontsize property of the axes objects
linkaxes([axes_handle_1 axes_handle_2], 'y');
linkprop([axes_handle_1 axes_handle_2], 'FontSize');
%Set the x range limits and tick mark positions of the first axes object
set(axes_handle_1, 'XLim', [1 21], ...
'XTick', [1 5 10])
%Set the x range limits and tick mark positions for the second axes object.
%Also set the background color to 'none', which makes the background
%transparent.
set(axes_handle_2, 'Color', 'none', ...
'YTickLabel', [], ...
'XLim', [14 35], ...
'XTick', [25 30 35])
It's quite straightforward and to my knowledge there is no built-in way to do it otherwise, except maybe with submissions from the File Exchange. Anyhow if you have questions about the above code please ask!
Please use gca property of matlab. In gca you can set a variable as your scales. Make that variable by merging two different scales
x=[1:80];
y=[.1:.1:8];
figure
plot(x,y);
%First Scale
scale1=[.1:.1:1];
%New scale is being started from 3. If we start from 1, 1 will be repeated
scale2=[3:2:9];
%Merging two variables scale1 and scale2
set(gca,'YTick',[scale1 scale2]);
Please refer http://www.mathworks.in/help/matlab/creating_plots/change-tick-marks-and-tick-labels-of-graph.html
You can also try the idea of scaling one dataset so that it has a similar magnitude as the other data set. Here you can multiply one dataset by 100 (or any suitable scaling parameter), and then it will be similar in size to the other data set. In order to clearly mention which data has been scaled in the graph use the legend.
plot(x,data1,x,100*data2)
legend('data1','100*data2','location','southeast')
Hope this helps.
I need to plot the following functions in matlab
y1=sign(x)
y2=tanh(x)
y3=(x)/(x+1)
The x-range is -5,5 with 0.1 spacing
The y-plot range should be between -1.5 to 1.5.
Each plot should have a labeled x and y axis and a legend in the lower right corner.
The only things I cant figure out is how to adjust the y plot range. Ive tried editing the actual figure but all that seems to do is distort the graph.
Is there a command within matlab that will let me adjust the y axis plot range?
The other thing I havent figured out yet is adding a legend, I can do it after the figure is created but I guess it needs to be done by matlab command.
Yes, use axis after the plot command:
axis([-5 5 -1.5 1.5])
If you only want to set the y-range without setting the x-range you can use
ylim([-1.5 1.5])
or alternatively axis([-inf inf -1.5 1.5]). I found this from the original MATLAB-source: https://de.mathworks.com/help/matlab/ref/ylim.html
PS: For trigonometric functions I would recommend to use axis equal to have equally spaced x and y-axis (see MATLAB)