Specifying color as hex works for plot, but not for scatter
47 views (last 30 days)
Show older comments
I was caught off-guard that specifying color as a hexadecimal value works for plot(), but not for scatter(). It does seem documented, but I could not think of any fundamental reason why it shouldn't work.
(I'm not sure this is a question. 😊)
N = 500;
color_hex = '#ff0000';
color_rgb = hex2rgb(color_hex);
x = rand(N,1);
y = rand(N,1);
figure
plot(x,y,'.',"Color",color_rgb)
figure
plot(x,y,'.',"Color",color_hex)
figure
scatter(x,y,[],color_rgb)
figure
scatter(x,y,[],color_hex)
1 Comment
dpb
on 8 Oct 2025 at 16:31
Edited: dpb
on 8 Oct 2025 at 19:55
One can replace scatter with plot by specifying the 'linestyle' as the marker with no line as your examples show. There's the discrepancy in that for scatter the size is proportional to area while in plot it is diameter, but one can easily make the scaling change to duplicate(*). The more pain to reproduce is that plot() can only have one size or color for the line so must create the whole array of lines if want to draw the markers by size of the data or color by size/type. Thus scatter() has the real advantage in that case of only the one graphics object handle instead of N.
I see no justification for the restriction and indeed, the illustration of the color map in the current documentation uses the same table including the hex definition column just as in plot (although note that the triplet linestyle doesn't).
One difference is that scatter uses 'CData' instead of 'Color', but there doesn't seem any fundamental reason either can't be specified as either hex or rgb; it's just mapping the same number into a triplet.
The only other difference I can think of that may be of note is that scatter can also accept a vector of colormap indices that plot/line can't. Again, I don't see why that would prohibit passing the hex color; it certainly wouldn't be difficult to detect the hex string while parsing the input.
I guess it would be a not unreasonable enhancement...albeit not critical.
Is an interesting anomaly, however, wonder why it started out that way and never caught up?
(*) Code to duplicate scatter appearance with plot()...while both draw the same-sized marker by default, the default size for scatter() is the square of the default size of plot().
N = 25;
color_hex = '#ff0000';
color_rgb = hex2rgb(color_hex);
x = rand(N,1);
y = rand(N,1);
tiledlayout(2,2)
nexttile
hSc=scatter(x,y,[],color_rgb);
axis square
nexttile
SZ=sqrt(hSc.SizeData); % plot proportional to diameter instead of area...
hL=plot(x,y,'o','Color',color_rgb);
box off
axis square
Answers (1)
Steve Eddins
on 13 Oct 2025 at 14:05
I believe this is a bug, and I recommend that you report it to MathWorks support. Perhaps it was an oversight from when hex color codes were added as a form of color specification.
It is at least a documentation bug. In the doc page for scatter, you can see a discrepancy in the description for "c — Marker color." The bulleted list of ways to specify c does not include hex colors:

But that same section then shows a table that includes hex colors:

The doc page "Specify Plot Colors," which explains generally how to specify colors in MATLAB graphics, includes hex colors in the bulleted list of forms, as well as the table. That page uses the bar and scatter functions in its examples, but the examples for scatter omit the hex form.
So, there are, at least, some doc page inconsistencies and misleading omissions. But I believe the design intent was to support hex colors in all core MATLAB graphics functions, and therefore I think this may be a development bug, in the sense that it seems to be a failure to implement fully the intended design.
Perhaps there was a problem implementing hex support within the existing syntaxes for scatter, although it is not obvious to me what that problem might have been.
0 Comments
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!