How to specify Hexadecimal Color Code on a fit line?

81 ビュー (過去 30 日間)
Goncalo Costa
Goncalo Costa 2022 年 3 月 25 日
回答済み: Jan 2022 年 4 月 5 日
I have been able to present a fit line through a scatter plot by writting the following, in a for loop:
t_array: time array
P2P: data array
colour_shape = {'b', 'y' , 'g', 'r'};
for x0 = 1:4
trial2 = colour_shape{x0};
fit_line = fit(t_array, P2P, 'exp2'); %fit function
figure(5)
p(x0) = plot(t_array, P2P, 'o');
hold on
plot(fit_line, trial2)
end
The colours that MATLAB automatically chose for the scatter plot, p(x0) = plot(t_array, P2P, 'o') , are the default ones and are the ideal. I tried using these same default colour for plotting the fit_line, by substituting the colour_shape cell values by:
colour_shape = {'#0072BD' , '#EDB120' , '#77AC30' , '#A2142F'};
But I get the following statement: "Invalid color or line style."
How can I use these colour for the best-fit line? How can I also increase the thickness of these lines?
  4 件のコメント
Stephen23
Stephen23 2022 年 3 月 25 日
What do you expect this indexing:
colour_shape{x0}
to achieve on a 1x9 character vector?
Goncalo Costa
Goncalo Costa 2022 年 3 月 25 日
The way I hae written it on my previous comment worked, I had a mistake in the way it was written on MATLAB, but not on the way it was written here, my apologies for that.

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

採用された回答

Santosh Fatale
Santosh Fatale 2022 年 4 月 5 日
Hi Goncalo,
I understand that you want to plot datapoints in figure and fit a line using “fit” function from “Curve Fitting Toolbox”. You want to customize the colour of the fitted line using Hex-Code.
The following code snippet gives the desired result.
colour_shape = {'#0072BD', '#EDB120', '#77AC30', '#A2142F'};
for x0 = 1:4
trial2 = colour_shape{x0};
fit_line = fit(t_array, P2P, 'exp2'); %fit function
figHandle = plot(fit_line,t_array, P2P,'o'); % fit_line and datapoints can be plotted using single command.
set(figHandle(2),'Color',colour_shape{x0});
end
Note that t_array and P2P must be a column vector or matrix for passing as inputs to the “fit” function. In the above code, figHandle is the handle to the graphics object created using plot function.
>> figHandle
figHandle =
2×1 Line array:
Line (data)
Line (fitted curve)
For more info, refer to the documentation of plot function with Curve Fitting Toolbox.

その他の回答 (1 件)

Jan
Jan 2022 年 4 月 5 日
This is working in modern Matlab versions:
plot(1:10, rand(1, 10), 'Color', '#A2142F')
For older versions:
color = uint8(sscanf('#A2142F', '#%2x%2x%2x'));
plot(1:10, rand(1, 10), 'Color', color);

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by