| function ShowHeart(Ntype) % a funny script to show different kind of heart curves % Usage: % ShowHeart() % Copyright: http://scriptdemo.blogspot.com % Ref: http://www.mathematische-basteleien.de/heart.htm % http://mathworld.wolfram.com/HeartCurve.html if nargin==0 Ntype=1; end np=999;Is3D=0;IsFilled=1; switch Ntype case {1} t=0:2*pi/np:2*pi; xx=(1-sin(t)).*cos(t); yy=(1-sin(t)).*sin(t); case {2} t=-1:2/(np*100):1; xx=sin(t).*cos(t).*log(abs(t)); yy=sqrt(cos(t)).*abs(t).^0.3; yy(isnan(xx))=[]; xx(isnan(xx))=[]; case {3} t=-1:2/(np*100):1; xx=sin(t).*cos(t).*log(abs(t)); yy=sqrt(abs(t)).*cos(t); yy(isnan(xx))=[]; xx(isnan(xx))=[]; case {4} t=0:2*pi/np:2*pi; r=sin(t).*sqrt(abs(cos(t)))./(sin(t)+7/5)-2*sin(t)+2; xx=r.*cos(t); yy=r.*sin(t); clear r case {5} t=0:60/np:60; r=0.01*(-t.*t+40*t+1200); xx=r.*sin(pi*t/180); yy=r.*cos(pi*t/180); clear r t xx=[xx fliplr(-xx)]; yy=[yy fliplr(yy)]; case {6} t=-1:2/(np*100):1; r=(1-abs(t)).*(1+3*abs(t)); xx=r.*sin(t); yy=r.*cos(t); clear r t otherwise disp(['Sorry, you just broke my heart!']); Is3D=1; t=0:2*pi/np:2*pi; r=sin(t).*sqrt(abs(cos(t)))./(sin(t)+7/5)-2*sin(t)+2; xx=r.*cos(t); yy=r.*sin(t); clear r end if ~Is3D if ~IsFilled plot(xx,yy,'color','r'); else hf=fill(xx,yy,'r');set(hf,'linestyle','none'); end else hp=plot(xx,yy,'g');set(hp,'linestyle','none'); figurenicer; hold on; myxlim=xlim;myylim=ylim; nrp=50; nxgrid=20; nygrid=20; xxn=linspace(myxlim(1),myxlim(2),nxgrid); yyn=linspace(myylim(1),myylim(2),nygrid); [xxn,yyn]=meshgrid(xxn,yyn); tmprand=randn(nrp,2); tmprand=tmprand/max(abs(tmprand(:)))/2; xxrand=zeros(nrp,nxgrid*nygrid); yyrand=xxrand; np=1; for Nx=1:nxgrid for Ny=1:nygrid xxrand(:,np)=xxn(Ny,Nx)+tmprand(:,1); yyrand(:,np)=yyn(Ny,Nx)+tmprand(:,2); np=np+1; end end Ind=inpolygon(xxn(:),yyn(:),xx(:),yy(:)); tmpind=1:length(xxn(:)); [jj,ii]=ind2sub(size(xxn),tmpind(Ind==1)); Ind2=sub2ind(size(xxn),jj(:)+1,ii(:)); Ind=sub2ind(size(xxn),jj(:),ii(:)); xxrand(:,Ind2)=xxrand(:,Ind); yyrand(:,Ind2)=yyrand(:,Ind); [xxnew,yynew]=GetRandom(xx(:),yy(:),5); hp0=plot(xxnew,yynew,'g.');set(hp0,'linestyle','none','markersize',1); %plot(xxn(:),yyn(:),'.b'); hp=plot(xxrand,yyrand,'.g');set(hp,'linestyle','none','markersize',1); end figurenicer; axis equal; axis tight; function [xxnew,yynew]=GetRandom(xx,yy,nrp) np=length(xx); if (np>360) iii=linspace(1,np,360); iii=fix(iii); xx=xx(iii); yy=yy(iii); np=length(xx); end tmprand=randn(nrp,2); tmprand=tmprand/max(abs(tmprand(:)))*8; xxnew=zeros(nrp,length(xx)); xx=reshape(xx,1,[]); yy=reshape(yy,1,[]); xxdif=diff(xx);xxdif=[xxdif(1) xxdif]; yydif=diff(yy);yydif=[yydif(1) yydif]; for N=2:length(xx) xxnew(:,N)=0.5*(xx(N-1)+xx(N))+tmprand(:,1)*xxdif(N); yynew(:,N)=0.5*(yy(N-1)+yy(N))+tmprand(:,2)*yydif(N); end xxnew(:,1)=xxnew(:,2); yynew(:,1)=yynew(:,2); |
Showing posts with label plot. Show all posts
Showing posts with label plot. Show all posts
Wednesday, October 13, 2010
Plot a heart curve in matlab
Labels:
figure,
heart curve,
Matlab,
plot,
random,
script,
visualization
Friday, July 23, 2010
matlab 3d plot with a map background
load korea
load geoid
t=0:pi/100:10*pi;
xx=100+50*sin(t);
yy=60+50*cos(t);
pcolor(map);shading interp; colorbar
colormap(demcmap(map))
cmap=colormap;
for N=1:length(xx)
C(N,1:3)=cmap(max(1,mod(N,64)),:);
end
hold on;
scatter3(xx(:),yy(:),t(:),10,C);
view(3)
Tuesday, February 16, 2010
Time series plot with a zoom window
| function [hax,hline]=PlotLineZoom(xx,yy,xx1,MyFillC); % Plot a time series with a zoom window % usage: % [hax,hline]=PlotLineZoom(xx,yy,xx1) % http://scriptdemo.blogspot.com % The whole time series hax(1)=axes('position',[0.13 0.13 0.77 0.72]); hline(1)=plot(xx,yy); xdiff05=0.5*max(diff(xx)); set(hline(1),'linewidth',2,'color','k','linestyle','-','marker','none'); set(hax(1),'tickdir','out','xminortick','on','yminortick','on','fontweight', ... 'bold','fontsize',14,'xlim',[xx(1)-xdiff05 xx(end)+xdiff05]); % zoomed region NX1=find(xx==xx1(1)); NX2=find(xx==xx1(end)); yy1=yy(NX1:NX2); MaxY=max(yy1); MinY=min(yy1); if ~ishold hold on; end if exist('MyFillC','var') hf=fill([xx1(1) xx1(end) xx1(end) xx1(1) xx1(1)],[MinY MinY MaxY MaxY MinY],MyFillC); set(hf,'linestyle','none','facealpha',0.6); else plot([xx1(1) xx1(end) xx1(end) xx1(1) xx1(1)],[MinY MinY MaxY MaxY MinY],'r','linewidth',2); end % zoom window hax(2)=axes('position',[0.5 0.2 0.3500 0.2000]); hline(2)=plot(xx1,yy1); set(hline(2),'linewidth',2,'color','r','linestyle','-','marker','o', ... 'markersize',6,'markerfacecolor','r','markeredgecolor','k'); set(hax(2),'color','none','tickdir','in','box','on','fontweight','bold', ... 'fontsize',10,'xtick',xx1,'xlim',[xx1(1) xx1(end)+0.2*xdiff05]); % figure properties set(gcf,'color','w'); |
Labels:
Matlab,
plot,
script,
visualization,
脚本
Subscribe to:
Posts (Atom)



