3

I was trying to make the bars in my scatterhist plot be of the same color as the markers:

x = randn(1,20);
y = randn(1,20);
myColour = [1 0 0]; % red
scatterhist(x, y, 'Color', myColour);
mygca = get(gca,'children');
set(mygca,'markerfacecolor', myColour);

However, the bars are of a slightly different color, namely a reddish hue, [249 96 96]: enter image description here

The Scatterhist documentation seems to suggest bar colors just follow marker color, which in this case does not happen.

How can I control color of the scatterhist bars, on MATLAB R2016a?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
z8080
  • 571
  • 3
  • 7
  • 23

1 Answers1

6

This happens because the bars have an alpha (transparency) setting.

To fix this, make sure the 'FaceAlpha' setting is set to 1. For example:

x = randn(1,20);
y = randn(1,20);
myColour = [1 0 0];
hSh = scatterhist(x, y, 'Color', myColour);
hSh(1).Children.MarkerFaceColor = myColour;
hSh(2).Children.FaceAlpha = 1;
hSh(3).Children.FaceAlpha = 1;

Which yields:

Proper colors

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • how do you set the transparency on the markers too?? – Oliver Amundsen Oct 30 '19 at 12:53
  • @OliverAmundsen as explained in [this blog post](http://undocumentedmatlab.com/blog/plot-markers-transparency-and-color-gradient), `hMarkers = hSh(1).Children.MarkerHandle; hMarkers.FaceColorType = 'truecoloralpha'; hMarkers.FaceColorData = uint8(255*[1;0;0;0.3]);`. It's best if you asked a separate question, and linked to this post for context. – Dev-iL Oct 30 '19 at 14:28
  • I created a question, please take a look here: https://stackoverflow.com/questions/58628007/controlling-scatterhist-marker-transparency – Oliver Amundsen Oct 30 '19 at 16:40