![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177084/image.png)
Creating a code based on a figure?
48 次查看(过去 30 天)
显示 更早的评论
I am trying to basically copy this graph for practice for my final coming up but I don't understand how to change the font,size or labeling the axis. Simply put, I need to replicate this graph exactly from my code. I need the font to be times new roman and size 18 with a marker size of 8.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146408/image.png)
This is my code:
clear
clc
x = linspace(0,2);
y1 = sin(2*pi*x);
y2 = exp(-0.5*2*pi*x).*sin(2*pi*x);
figure
subplot(2,1,1);
hPlot1 = plot(x,y1,'rs');
%// The important part.
set(gca,'YLim',[-1 2],'YTick',-1:1:2,'XTick',0:.5:2)
subplot(2,1,2);
hPlot2 = plot(x,y2,'k*');
set(gca,'YLim',[-0.2,0.6],'YTick',[-0.2,0,0.2,0.4,0.6],'XTick',0:.5:2)
0 个评论
回答(2 个)
Mischa Kim
2014-12-11
Stavo, one thing you can do is run your code and then edit the plot manually by choosing "Show Plot Tools...", the symbol in the right corner of the plot window.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177084/image.png)
Once you have finished editing choose "Generate Code" in the "File" menu of the plot window. This will create the corresponding MATLAB code similar to yours but adapted to your requirements.
Image Analyst
2014-12-28
This gets you most of the way there.
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 = 20;
x = linspace(0,2, 100);
y1 = sin(2*pi*x);
y2 = exp(-0.5*2*pi*x).*sin(2*pi*x);
subplot(2,1,1);
plot(x,y1,'r-', 'LineWidth', 2); % Red line.
hold on;
plot(x(1:6:end),y1(1:6:end),'ks', 'MarkerSize', 10, 'LineWidth', 2); % Black squares.
ylim([-1,2]);
ylabel('f(t)', 'FontSize', fontSize);
text(.5, 1.2, 'Harmonic Force f(t) = sin(wt)', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%// The important part.
set(gca,'YLim',[-1 2],'YTick',-1:1:2,'XTick',0:.5:2)
subplot(2,1,2);
plot(x, y2, 'k-', 'LineWidth', 2); % Black line
hold on;
plot(x(1:6:end),y2(1:6:end),'r*', 'MarkerSize', 10, 'LineWidth', 2); % Red Stars.
ylim([-.2,.6]);
ylabel('g(t)', 'FontSize', fontSize);
xlabel('Time (s)', 'FontSize', fontSize);
text(.45, .3, 'Forced Response g(t) = exp(-zetawt)*sin(wt)', 'FontSize', fontSize);
set(gca,'YLim',[-0.2,0.6],'YTick',[-0.2,0,0.2,0.4,0.6],'XTick',0:.5:2)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172660/image.jpeg)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!