When using xline, would it be possible to have the text in one color and the lines in another color?

31 ビュー (過去 30 日間)
When using xline, would it be possible to have the text in one color and the lines in another color?
% Here, I would like to have the text in one color and the vertical lines in
% another color
fig = figure();
ax = axes(fig);
ax.XTick = 1:14;
hold on
arrayfun(@(x)xline(x,'-','S','LabelOrientation','horizontal','Color',[.5 .5 .5]), 0:13)

採用された回答

Star Strider
Star Strider 2023 年 3 月 17 日
Doing that in one pass is likely not an option, since there is no way to independently control the text colour. However colouring the labels with one xline call and the lines with a second xline call is certainly possible —
fig = figure();
ax = axes(fig);
ax.XTick = 1:14;
dy = ["S","S","M","T","W","T","F"];
xline(ax.XTick-1,'-k',repmat(dy,1,fix(max(ax.XTick)/7)), 'LabelOrientation','horiz','Color',[.9 .7 .5]);
xline(ax.XTick-1,'-k')
fig = figure();
ax = axes(fig);
ax.XTick = 1:14;
cm = colormap(turbo(max(ax.XTick))); % Define 'colormap'
dy = ["S","S","M","T","W","T","F"];
dyv = repmat(dy,1,fix(max(ax.XTick)/7)); % 'dyv': Days Vector
arrayfun(@(idx)xline(idx-1,'-k', dyv(idx), 'LabelOrientation','horiz','Color',cm(idx,:)),ax.XTick);
xline(ax.XTick-1,'-k')
The first approach uses the same colour for each day, and the second a different colour for each day. Any colormap will work.
The same cautions apply here, specifically that the elements of ‘cm’ and ‘dyv’ have to be consistent with the value range of ax.XTick.
.
  3 件のコメント
Sim
Sim 2023 年 3 月 17 日
編集済み: Sim 2023 年 3 月 17 日
Whether it would be of any use to other Matlab users, here there is a small variation of the @Star Strider code, where we can put the xlines in some customized position, and not following the ax.XTick:
clc
fig = figure();
ax = axes(fig);
a = [1 7 12 19 27 34 41 49]; % <-- customized position of xlines
ax.XTick = 1:max(a);
cm = colormap(turbo(max(ax.XTick)));
dyv = repmat(["S","S","M","T","W","T","F"],1,ceil(max(a)/7));
y = 1:length(a);
arrayfun(@(x,y)xline(x,'-k', dyv(y), 'LabelOrientation','horiz','Color',cm(x,:)),a,y);
xline(a,'-k')
xlim([0 max(a)])

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

その他の回答 (1 件)

Afiq Azaibi
Afiq Azaibi 2024 年 10 月 4 日
Starting in R2024b, you can leverage the LabelColor property on ConstantLine to control the color of the label independently of the line.
colors = turbo(7);
for i = 1:7
xline(i, '-k', 'ABC', 'LabelOrientation', 'horizontal', 'LabelColor', colors(i,:))
end

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by