Why plot is over plotted?
显示 更早的评论
%i degree for curve fitting
%j is the iterating number for subplot title
x=[1:10]
y=sin(x)
for i=10:10:100
title([' degree = ' ,num2str(i)])
for j=1:10
subplot(10,1,j)
p= polyfit(x,y,i)
x2=[1:0.01:10]
y2=polyval(p,x2)
plot(x,y,'o',x2,y2,'-b')
hold on
end
end
采纳的回答
x = 1:10; % No need for square brackets
y = sin(x);
for k = 1:10
% NextPlot='add' is the same as "hold on"
AxesH(k) = subplot(10, 1, k, 'NextPlot', 'add');
end
x2 = 1:0.01:10;
for i = 10:10:100
for j = 1:10
p = polyfit(x,y,i);
y2 = polyval(p,x2);
plot(AxesH(j), x,y,'o', x2,y2,'-b');
end
end
subplot clears formerly existing axes objects. So create the axes at first and use them as parent object as first input to plot.
7 个评论
But I want to see degree of polynomial in each subplot?
Please explain it. What do you want to see where? In your original code you had a
title([' degree = ' ,num2str(i)])
But this was overwritten in each iteration over i. I do not understand the purpose of the inner loop over j at all. Is it intended that all diagrams have the same contents?
I want to see the degree of polynomial in each subplot and eac subplot is curve fitted according to i , I don’t know how use loop in this scenario , if you could help me debug the code I would be so thankful.
I want to see the degree of polynomial in each subplot
Please, madhan ravi, remember that I cannot read your mind. How do you want to "see" this? By a legend and lines in different colors? By a title above each subplot? By different markers for the lines? By annotations or text beside the lines?
I cannot help you to debug the code, when it is not clear, what the bug is. What do you want to achieve?
Sorry jan for being unclear . All I want to do is like the example below: T The first subplot title should be 10.
The second subplot title should be ‘20’.
The third subplot title should be ‘30’
The fourth subplot title should be ‘40’.
The fifth subplot title should be ‘50’
The sixth subplot title should be ‘60’
The seventh ‘70’
The eighth ‘80’
The nineth ‘90’
The last ‘100’.
This is what I need .
I highly appreciate your desperation to help me.
You are welcome, madhan ravi. This is a forum for Matlab questions :-)
Insert a line to draw the title in the first loop to create the axes:
for k = 1:10
% NextPlot='add' is the same as "hold on"
AxesH(k) = subplot(10, 1, k, 'NextPlot', 'add');
title(AxesH(k), sprintf('%d', k * 10));
end
Does this help?
madhan ravi
2018-7-12
编辑:madhan ravi
2018-7-12
Awesome!! Thank you so much @jan.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 2-D and 3-D Plots 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
