How to add pattern fill to markers in scatter plot?

20 views (last 30 days)
I want to add pattern fill to markers in scatterplot. It can easily be done in Excel (example befow). Is it possible to reproduce this in Matlab? I have seen user-defined functions in the file exchange like hatchfill2 but they seem to work well on bar plots and patch objects but not markers in scatter plot.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Nov 2024
There is unfortunately no simple way to add hatch filling.
To apply hatch filling, there are two possibilities:
  1. Create sample copies of the filled area, centered on (0,0) and extending to [-1, 1] . Now create one hg transform object for each destination location, and copyobj() the sample object to parent it to the hg transform object, and set the Matrix property of the hg transform object to scale and translate it to the desired position. This approach has the advantage of only having to figure out the hatch pattern once per different hatch. This approach has the disadvantage of requiring separate copies of the objects and seperate hg transform objects for each desired location.
  2. Create extensive Vertices and Faces lists combining all of the hatch objects to be created, and create a single patch() object that combines information for all of the hatches together. This approach has the advantage of only using a single graphics object. This approach has the disadvantage of making it more difficult to program the hatch objects and to program different colors for the different objects.
  2 Comments
Adam Danz
Adam Danz on 8 Nov 2024
3. A workaround to providing a hatch is to duplicate the scatter markers with + - | . x * symbols. This won't unfortunately, show up in the legend, though.
hold on;
s = scatter(rand(1,20),sort(rand(1,20)),randi(50,1,20)+40, 'c','filled','MarkerEdgeColor','k');
s2 = copyobj(s,s.Parent);
s2.Marker = '+';
p = scatter(rand(1,20)+.8,sort(rand(1,20))+.3,randi(50,1,20)+40, 'm','filled','MarkerEdgeColor','k');
p2 = copyobj(p,p.Parent);
p2.Marker = '.';
Elmira Ramazanova
Elmira Ramazanova on 11 Nov 2024 at 18:14
This workaround with + - | . x * symbols helped. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!