Representing vector field with ovals and color

I have a vector field which shows the direction of some ovals in different positions with different aspect ratio. I could plot it in Matlab by the below script:

[X, Y] = meshgrid(1:size(nx,1), 1:size(nx,2));
  quiver(X,Y, nx, ny, 'ShowArrowHead', 'Off','linewidth',1);
set(qh, 'Color', 'k');

Where X and Y show positions in space and nx and ny show orientation of the ovals. Information on aspect ratio is inside nx and ny. So, sqrt(nx.^2+ny.^2) gives a magnitude that is between 0 and 1. If this magnitude is zero in some points, it means that in those points we have circle instead of ovals. If it is equal to 1 it means we have an elongated oval. But it looks very basic. I want to have something in which each point in space I have a rotated oval where the color shows the aspect ratio. How can I do this in Matlab or python?

1 answer

  • answered 2020-06-02 15:24 obchardon

    You can create a grid of ellipse and apply a rotation matrix to each of them. Here is a minimal example:

    [X0, Y0] = meshgrid(0:5,0:5);  % the initial grid
    a        = 0.2;                % horizontal radius
    b        = 0.4;                % vertical radius
    t        = 0:0.01:2*pi;        
    rotmat   = @(phi) [cos(phi) sin(phi); -sin(phi) cos(phi)]; % A 2D anonymous rotation matrix 
    
    hold on
    % For each point of the grid we plot an ellipse
    for ii = 1:numel(X0)
        phi = mod((ii/20),2*pi);               % rotation by phi (in radian)
        XY  = rotmat(phi)*[a*cos(t);b*sin(t)]; % we apply the rotation to the ellipse equation
        x   = XY(1,:)+X0(ii);                  % Shift x with X0
        y   = XY(2,:)+Y0(ii);                  % Shift y with Y0
        fill(x,y,ii)                           % plot the ellipse
    end
    axis equal
    

    And we obtain:

    enter image description here

    You just need to change the horizontal and vertical radius and phi according to nx and ny.