Change Arrowhead size in quiver plot OCTAVE - matlab

I am making a simple quiver plot, but due to arrowhead size depending on the length of arrows, The arrowheads become so small that the arrow is not practically visible. I want to scale up the arrowheads to one universal size (all arrows should have the same arrowhead size in this quiver plot.
A=5;
[X,Y] = meshgrid(-3:0.5:3,-3:0.5:3);
U = A*(X.^2-Y.^2)./((X.^2+Y.^2).^2);
V = (2*A*X.*Y)./((X.^2+Y.^2).^2);
figure(1)
h = quiver(X,Y,U,V,10);
set(h,'MaxHeadSize',0.1,'AutoScale','on', 'AutoScaleFactor', 2)
This is how my plot looks:
This is how the Arrowheads should look like in size

Related

How to scale vertical axis in increasing order from Bottom to Top in Matlab?

clear all;close all;clc
x =linspace(pi,2*pi,20)
y =linspace(0,pi,20)
C = [1 2];
imagesc(x,y,C)
colorbar
The vertical axis is not increasing from bottom to top.
May I know how to fix this? I have tried using flipr() so that vertical axis increase from bottom to top.
You are using an image, thus the axis are in image mode, where the coordinates start on the top left.
imagesc and other image showing functions automatically call axis image;. You can change that to the normal axis by calling axis xy; after the imagesc call.
You can also change the direction of the y axis (or any axis) by changing the YDir property.
% Get the handle to the axes
ax = gca;
% Change the y axis direction
ax.YDir = 'normal';

Filling an area below a curve with a colormap defined by the function of the curve

Consider the following plot:
On the left you can see the fill of a circle with respect to a profile of a function using patch command
t = linspace(-pi,pi,100);
c = exp(-cos(t));
figure(1)
patch(cos(t),sin(t),c)
axis equal
On the right you see the profile of the function along the dashed line axis on the left, which is filled using the area command.
figure(2)
area(cos(t),c,0);
what I'm trying to do is fill the area below the curve (right panel) with the colors defined by the colormap of the representation in the left panel. The results should look like this
The closest thing I could come up with is this:
function q55322965
% Evaluate the equation (half domain!)
t = linspace(-pi,0,50);
c = exp(-cos(t));
% Turn vectors into a mesh:
[TT,CC] = meshgrid(cos(t),c);
% Clear all points that are above the curve:
CC(CC > c) = NaN;
% Fill in the rectangle between the chart and zero:
CC(end+1,:) = 0;
TT(end+1,:) = TT(end,:);
% Plot:
figure(); mesh(TT,CC,CC,'FaceColor','interp','EdgeColor','interp'); view([0,90]);
Which yields:
If you want a less jagged look when plotting with this method, you can increase the resolution in t. For example, if we use 500 instead of 50 in the linspace we get:

How to keep plot size constant for two plots?

I'm using gramm to make two plots. I'm currently using
figure('position',[0,0,1000,1000])
So that each plot is graphed into the same size window. All of my fonts are the same size. However, plot A's X axis labels are shorter words than plot B's. This is causing matlab to shrink the size of the axis in plot B so that it can fit inside the window, which is causing plot A and B to be different sizes:
The actual images are the same size. However, the words take up more space in plot B, and so the actual plot is smaller. How can I tell matlab to keep the actual plots the same size?
Thanks in advance.
You can set the size for axis object, so change the axis size of Plot A to the axis size of plot B.
Click on Plot B to select it:
axisB = gca; % axis object for plot B
axisB.Units = 'pixels'; % or other absolute unit. Default is relative to figure
Now click on Plot A to select it:
axisA = gca;
axisA.Units = 'pixels'; % set to same unit as axisB
dHeight = axisA.Position(4) - axisB.Position(4);
axisA.Position(4) = axisB.Position(4); % set height of axisA to that of axisB
Optionally, you can reduce the figure size for plot A, so it looks better:
figA = gcf;
figA.Position(4) = figA.Position(4) - dHeight;

Align inset in matlab plot to the right

How can I align an inset of a MATLAB plot to the top right edge of the box, like in the picture?
The example was generated with GNU R as explained in How to add an inset (subplot) to "topright" of an R plot?
Here is a way to do it:
Basically create a figure with an axes, and then add a new axis that you place to a specific position and to which you give the size you want.
Code:
clc
clear
close all
%// Dummy data
x = -20:0;
x2 = x(5:10);
%// Zoomed region to put into inset.
y = x.^2;
y2 = y(5:10);
%// Create a figure
hFig = figure(1);
%// Plot the original data
hP1 = plot(x,y);
xlabel('Original x','FontSize',18)
ylabel('Original y','FontSize',18)
hold on
%// Add an axes and set its position to where you want. Its in normalized
%// units
hAxes2 = axes('Parent',hFig,'Position',[.58 .6 .3 .3]);
%// Plot the zommed region
plot(x2,y2,'Parent',hAxes2)
%// Set the axis limits and labels
set(gca,'Xlim',[x(5) x(10)])
xlabel('Zoomed x','FontSize',16)
ylabel('Zommed y','FontSize',16)
And output:
To be fancy you could play around with the new axes position so that the outer borders coincide with the large one, but that should get you going :)

How can I put a marker on the minimum point within a MATLAB figure?

I have a curve in which the minimum point is not obvious to the naked eye. For that reason I'm looking to highlight the minimum point using a marker.
Ideally I would highlight the point with a marker and also have its coordinates displayed in text on the figure.
You can do it this way:
%// Example plot
x = 1:10;
y = randn(1,10);
plot(x,y)
%// Marker at minimum
[ymin imin] = min(y);
xmin = x(imin);
hold on
style = 'ro'; %// red circle. Change as needed
markersize = 10; %// change as needed
plot(x(imin), ymin, style, 'markersize', markersize)
%// Text with coordinates of minimum
offset = -.05; %// vertical offset as a fraction of y-axis span. Change as needed.
text(x(imin),ymin+diff(ylim)*offset,['(' num2str(x(imin)) ',' num2str(ymin) ')'])
%// Enlarge y axis so that text is properly seen, if offset is negative
ylim(ylim+[diff(ylim)*offset*(offset<0) 0])
You might also want to enlarge the x axis if the text is close to the left or to the right. It could be done with xlim in a similar way.
Assuming you know the coordinates of that point, you can do something like:
hold on; % add things to the current figure
plot(x_coord, y_coord, '+r')
This will put a red plus sign at that point.
This should plot the minimum point(s), assuming you have data like y and x,
plot(x(y==min(y)),min(y),'o')
adding the text could be trickier depending on what you want, but at least these are the coordinates.