2D density plot with colorbar

7 vues (au cours des 30 derniers jours)
Farai Mahachi
Farai Mahachi le 9 Mai 2019
Hi guys,
I have data which has produced a 2D plot as shown in the first figure.
result_3.jpg
How can I transform the above figure to appear like the one below?
result_4.png

Réponse acceptée

KSSV
KSSV le 9 Mai 2019
Modifié(e) : KSSV le 9 Mai 2019
N = 100 ;
x = linspace(0,2*pi,N)' ;
y = sin(x) ;
xi = linspace(min(x),max(x),N) ;
yi = linspace(min(y)-1,max(y)+1,N) ;
[X,Y] = meshgrid(xi,yi) ;
Z = zeros(size(X)) ;
idx = knnsearch([X(:) Y(:)],[x y]) ;
Z(idx) = 1 ;
Z(idx+1) = 1 ;
Z(idx-1) = 1 ;
pcolor(X,Y,Z)
shading interp
colorbar
colormap(jet)
  1 commentaire
Farai Mahachi
Farai Mahachi le 9 Mai 2019
Modifié(e) : Farai Mahachi le 9 Mai 2019
Thanks KSSV,
does this procedure work as well for plotting multiple images on the same plot using pcolour and hold on?
I have been trying that but cant seem to plot two or more plots on the same figure.
I basically took your code and assigned different variables.
N = 100 ;
x = linspace(0,2*pi,N)' ;
y = sin(x) ;
xi = linspace(min(x),max(x),N) ;
yi = linspace(min(y)-1,max(y)+1,N) ;
[X,Y] = meshgrid(xi,yi) ;
Z = zeros(size(X)) ;
idx = knnsearch([X(:) Y(:)],[x y]) ;
Z(idx) = 1 ;
Z(idx+1) = 1 ;
Z(idx-1) = 1 ;
pcolor(X,Y,Z)
hold on
shading interp
colorbar
NN = 100 ;
xx = linspace(0,2*pi,NN)' ;
yy = cos(xx) ;
xi1 = linspace(min(xx),max(xx),NN) ;
yi1 = linspace(min(yy)-1,max(yy)+1,NN) ;
[XX,YY] = meshgrid(xi1,yi1) ;
ZZ = zeros(size(XX)) ;
idx1 = knnsearch([XX(:) YY(:)],[xx yy]) ;
ZZ(idx1) = 1 ;
ZZ(idx1+1) = 1 ;
ZZ(idx1-1) = 1 ;
pcolor(XX,YY,ZZ)
shading interp
colorbar
colormap(jet)
But cant seem to plot two plots on the same figure.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Formatting and Annotation dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by