Tuesday, April 11, 2017

Creating and customizing 3D graph in MATLAB.

Unlike 2D figure, 3 variables are involved in a 3D figure. Below is an example of 3D mesh graph. For certain value of X and Y, we plot the value of Z.
Example :
        Z = (X  /  (c*Y))
3D figure can be bar, mess, surf etc...

Let's begin drawing the graph for the equation
        Z = (X  /  (c*Y))

Step-1: Prepare the data

X = [0.1000    0.1889    0.2778    0.3667    0.4556    0.5444    0.6333    0.7222    0.8111    0.9000]
Y = [-0.1000   -0.1889   -0.2778   -0.3667   -0.4556   -0.5444   -0.6333   -0.7222   -0.8111   -0.9000]
c = 0.05

Here I have used the following Matlab code to generate the data for X and Y.
X = linspace(0.1,0.9,10)
Y = linspace(-0.1,-0.9,10)

for X, 0.1 is the initial value, 0.9 is the final value, and 10 is the number of values to be generated between initial and final value. Similarly for Y also. 


Now In order to generate the data for 3D surf convert X and Y data to a meshgrid.
Step-2: 
           [mx, my] = meshgrid(X, Y);

Step-3: Generate data for Z-axis
           Z = mx ./ (c.*my);

Step-4: Get ready to plot the data onto a 3D figure.

Enter the following code


figure1 = figure();

% Create axes
axes1 = axes('Parent',figure1);
hold(axes1,'on');
% Create xlabel
xlabel('X-axis Label');

% Create ylabel
ylabel('Y-axis Label');

% Create zlabel
zlabel('Z-axis Label');

view(axes1,[152.1 22]);


% Create surf
surf(ax,ay,Z,'Parent',axes1);

grid(axes1,'on');

% Set the remaining axes properties
set(axes1,'FontName','Times New Roman','FontSize',14,'FontWeight','bold',...
    'XTick',[0.1 0.3 0.5 0.7 0.9],'XTickLabel',...
    {'0.1','0.3','0.5','0.7','0.9'});

Final Step:
The entire Matlab file will look like this

X=linspace(0.1,0.9,10);
Y=linspace(-0.1,-0.9,10);
c=0.05;

[ax, ay] = meshgrid(X,Y);
Z = ax ./(c.*ay);

figure1 = figure();

% Create axes
axes1 = axes('Parent',figure1);
hold(axes1,'on');
% Create xlabel
xlabel('X-axis Label');

% Create ylabel
ylabel('Y-axis Label');

% Create zlabel
zlabel('Z-axis Label');

view(axes1,[152.1 22]);


% Create surf
surf(ax,ay,Z,'Parent',axes1);

grid(axes1,'on');

% Set the remaining axes properties
set(axes1,'FontName','Times New Roman','FontSize',14,'FontWeight','bold',...
    'XTick',[0.1 0.3 0.5 0.7 0.9],'XTickLabel',...
    {'0.1','0.3','0.5','0.7','0.9'});


Bonus tips: Customizing the axis label with text() and annotation().

Disable xlabel(), ylabel(), and zlabel() if you are going for text() or annotation() command.

text() example:
% After running this .m file edit the position of each text box
   ty=text(0.5,0.5,'Y-axis label','Rotation',-28,'FontSize',20,...
    'FontWeight','bold','FontName','Times New Roman');

annotation() example:

  annotation(figure1,'textbox',...
     [0.528242677824266 0.789163935354498 0.0753138055985942 0.0475409826782883],...
     'String',{'? = 0.5'},...
     'FontWeight','bold',...
     'FontSize',12,...
     'FontName','Times New Roman',...
     'EdgeColor',[0.24705882370472 0.24705882370472 0.24705882370472],...
     'BackgroundColor',[1 1 1]);

Thats all for now. 
Please comment if more customization function you need.


Thank you


No comments:

Post a Comment