How to plot a magnification inside a figure
117 views (last 30 days)
Show older comments
Hi guys!
I've a plot in which I've drawn 2 functions and I want to magnificate the portion circled in red in figure below:
The code that I've used is the following (note that I'v obtained the figure reversing y-axis):
f3=figure (3);
plot(x,cp_inv,'k--',x,cp_5e6,'k-','LineWidth',1); %here there are 2 tabular functions, the x vector is the same
axis ([-0.05 1.05 -1.5 1.2])
set (gca,'Ydir','reverse');
xlabel ('x/c');
ylabel ('C_p');
grid on;
legend ('non viscoso','viscoso');
legend('Location','southeast');
Can anyone help me ?
0 Comments
Accepted Answer
Image Analyst
on 20 Dec 2019
Edited: Image Analyst
on 20 Dec 2019
See this snippet. Adapt it to plot your two functions just with indexes where x is between 0.45 and 0.55
% Draw a small plot inset to a larger one.
% Ref: https://www.mathworks.com/matlabcentral/answers/60376-how-to-make-an-inset-of-matlab-figure-inside-the-figure#comment_654093
clc; % Clear the command window.
% close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
x1 = linspace(0, 1);
x2 = linspace(3/4, 1);
y1 = sin(2*pi*x1);
y2 = sin(2*pi*x2);
figure(1)
% plot on large axes
plot(x1, y1, 'LineWidth', 2)
grid on;
ax1 = gca; % Store handle to axes 1.
% Create smaller axes in top right, and plot on it
% Store handle to axes 2 in ax2.
ax2 = axes('Position',[.7 .7 .2 .2])
box on;
plot(x2, y2, 'b-', 'LineWidth', 2)
grid on;
% Now draw something back on axis 1
hold(ax1, 'on'); % Don't blow away existing curve.
y1b = cos(2*pi*x1/3);
plot(ax1, x1, y1b, 'r-', 'LineWidth', 2);
% Now draw something back on axis 2
hold(ax2, 'on'); % Don't blow away existing curve.
y2b = cos(2*pi*x2/3);
plot(ax2, x2, y2b, 'r-', 'LineWidth', 2);
3 Comments
Image Analyst
on 21 Dec 2019
Guiseppe, of course you're not forced to reconstruct your x and y. You just use use your existing x and y and indexing to get the right location of those curves.
And it's essentially just two lines of code -- a call to axes() and a call to plot(). You can get rid of the 2 lines of code that call grid and box if you don't want those. So, no, it can't get any simpler than 2 lines of code. Here's some simplified code with the adaptations I hope you made (like plotting only your one curve instead of two as in my demo):
% Create sample data
numPoints = 400;
x = linspace(0, 1, numPoints);
y = sin(2*pi*x) + 0.1 * rand(1, numPoints);
% Now we have our sample data and we can begin plotting.
figure(1)
% plot on large axes
plot(x, y, 'LineWidth', 2)
grid on;
ax1 = gca; % Store handle to axes 1.
% Find indexes for x = 0.45 to 0.55.
index1 = find(x > 0.45, 1, 'first');
index2 = find(x > 0.55, 1, 'first');
% Optional put a red rectangle around the source
axes(ax1);
hold on;
height = abs(y(index1) - y(index2));
rectangle('Position', [x(index1), y(index1)-height, 0.1, height], ...
'EdgeColor', 'r', 'LineWidth', 2);
% Create smaller axes in top right, and plot on it
% Store handle to axes 2 in ax2.
ax2 = axes('Position',[.7 .7 .2 .2])
box on;
plot(x(index1:index2), y(index1:index2), 'b-', 'LineWidth', 2)
grid on;
What you need to look at are the last 4 lines of code. All the rest is just optional. Even the last 4 lines could be boiled down to 2 if you get rid of box and grid. Two lines of code is pretty simple. If you still have trouble, then attach your data and code and explain why it's not working.
More Answers (0)
See Also
Categories
Find more on Specifying Target for Graphics Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!