How to specify line color using a hexadecimal color code

조회 수: 1,097 (최근 30일)
XAXRXTX
XAXRXTX 2019년 4월 23일
댓글: Zainab Al-RubayezayMATH 2019년 8월 8일
Hi,
why the plot function return an error message when I specify the color as a hexadecimal color code i.e
x = linspace(-2*pi,2*pi);
y = sin(x);
plot( x , y , 'Color' , '#FF0000' )
Error using plot
Specified character vector is an invalid color value.

채택된 답변

Akira Agata
Akira Agata 2019년 4월 24일
It would be due to your MATLAB version. Hexadecimal color code can be used in the latest version (R2019a).
If your version is R2018b or older, you have to convert hex color code to 1-by-3 RGB array, like:
x = linspace(-2*pi,2*pi);
y = sin(x);
% Convert color code to 1-by-3 RGB array (0~1 each)
str = '#FF0000';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
figure
plot(x, y, 'Color', color)

추가 답변 (2개)

Subrata Paul
Subrata Paul 2019년 7월 30일
You can also use some color :
Matlab plotting colors
The following are the letters you can add to your code to control the color of your plot while plotting in Matlab.
  • b blue
  • g green
  • r red
  • c cyan
  • m magenta
  • y yellow
  • k black
  • w white
. point
o circle
x x-mark
+ plus
* star
s square
d diamond
v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
— dashed
-. dashdot
: dotted
– solid
Link: https://tutorial45.com/matlab-plot-colors-and-styles/

Subrata Paul
Subrata Paul 2019년 7월 30일
Thanks

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by