Plot a drawn, filled shape at each data point

5 ビュー (過去 30 日間)
DrEamonn
DrEamonn 2019 年 3 月 21 日
コメント済み: Star Strider 2019 年 3 月 22 日
I have a matrix which contains X & Y data for seperate, single point locations to be plotted on a map using a marker for each point (kind of like multiple 'X marks the spot' on a map)
This works well until I scale the figure window & then the markers become a lot smaller than the data they represent.
How would I plot a filled unit square centred on each data point e.g. if the first point to be plotted is at (50, 10) I would like to draw a square (49.5, 9.5) (50.5, 9.5) (50.5, 10.5) (49.5, 10.5) & fil the square in a particular colour?

回答 (3 件)

dpb
dpb 2019 年 3 月 21 日
編集済み: dpb 2019 年 3 月 21 日
S=1; % Side size
X=50; Y=10; % location
hR=rectangle('Position',[X-S/2,Y-S/2,S,S],'FaceColor','k');
xlim([X-5*S X+5*S]), ylim([Y-5*S Y+5*S])
hAx=gca;
grid on
  2 件のコメント
DrEamonn
DrEamonn 2019 年 3 月 21 日
Solution is almost there, but position vectors have multiple values i.e.
X = [10 20 30]
Y = [15 10 5]
I have tried creating the w & h elements as w = ones(size(X)) & passing the rectangle function
rectangle( X, Y, w, h)
dpb
dpb 2019 年 3 月 21 日
doc arrayfun
with anonymous function...
arrayfun(@(x,y) rectangle('Position',[x-S/2,y-S/2,S,S],'FaceColor'),'k',X,Y)
S is value set before definition.

サインインしてコメントする。


DrEamonn
DrEamonn 2019 年 3 月 21 日
rectangles function appears to do what I need
https://uk.mathworks.com/matlabcentral/fileexchange/59243-rectangles

Kelly Kearney
Kelly Kearney 2019 年 3 月 21 日
A single multi-faceted patch will render more quickly than lots of individual rectangles. And this option allows your marker to be any arbitrary shape:
X = [10 20 30]
Y = [15 10 5]
xsquare = [0 0 1 1 0]';
ysquare = [0 1 1 0 0]';
hrect = patch(X+xsquare, Y+ysquare, 'k')
  2 件のコメント
DrEamonn
DrEamonn 2019 年 3 月 22 日
Hi Kelly
I can see what you are suggesting to do, but I can't get it to work. Running your code gives me
Error using +
Matrix dimensions must agree.
Star Strider
Star Strider 2019 年 3 月 22 日
If you’re using R2016a or earlier (so do not have ‘automatic implicit expansion’) you need slightly different code:
Xxsq = bsxfun(@plus, X, xsquare)
Yysq = bsxfun(@plus, Y, ysquare)
hrect = patch(Xxsq, Yysq, 'k')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by