How do I change color of a text in a given subplot?
61 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a given figure which contains 2 subplots. the first subplot has a text on it. I want to change the text color and size using set/get parent/children commands... I already have the handle to the line in the top subplot(which is the first subplot as well). how can I do that? Is 'text' a parent or a child off the line in the top figure?
1 comentario
Respuestas (2)
sixwwwwww
el 7 de Dic. de 2013
Editada: sixwwwwww
el 7 de Dic. de 2013
you can change text color as follow:
x = -pi:.1:pi;
y = sin(x);
plot(x,y)
text(-pi/4, sin(-pi/4), '\leftarrow sin(-\pi\div4)', 'HorizontalAlignment','left', 'Color', 'r')
Or if you wanted to use set command then you can do it as follow:
h = text(-pi/4, sin(-pi/4), '\leftarrow sin(-\pi\div4)', 'HorizontalAlignment','left');
set(h, 'Color',[1, 0 ,0])
3 comentarios
sixwwwwww
el 7 de Dic. de 2013
Editada: sixwwwwww
el 7 de Dic. de 2013
Iy you already have handler 'h' for object whose color and font property you want to set then use:
set(h, 'Color',[1, 0 ,0], 'FontSize', 20)
Which text you want to change in that figure? Is it title or xlabel or ylabel?
Also in order to find some specific object in figure you can use 'findobj'. See for more information:
Johannes Hougaard
el 1 de Mayo de 2025
Every text in a figure has a handle which has a 'Color' property you can set individually for each of them or for all of them in one go
The easiest way to detect if something is a text is to check whether it contains a string. So I'd do it something like this to set them all in one go
figure;
subplot(2,1,1)
plot(1:999,sin(linspace(-8*pi,12*pi,999)));
text(200,0,"This is a text in subplot 1");
xlabel("X-axis 1")
ylabel("Y-axis 1")
subplot(2,1,2)
plot(1:999,cos(linspace(-4*pi,4*pi,999)));
text(200,0,"This is a text in subplot 2");
xlabel("X-axis 2")
ylabel("Y-axis 2")
all_handles = findall(gcf);
set(all_handles(isprop(all_handles,'String')),'Color',"#F76806");
And if you want to set them one by one you just go in a for loop or look for a specific string
e.g.
figure;
subplot(2,1,1)
plot(1:999,sin(linspace(-8*pi,12*pi,999)));
text(200,0,"This is a text in subplot 1");
xlabel("X-axis 1")
ylabel("Y-axis 1")
subplot(2,1,2)
plot(1:999,cos(linspace(-4*pi,4*pi,999)));
text(200,0,"This is a text in subplot 2");
xlabel("X-axis 2")
ylabel("Y-axis 2")
all_handles = findall(gcf);
string_handles = all_handles(isprop(all_handles,'String'));
subplot2text = contains({string_handles.String},"subplot 2");
set(string_handles(subplot2text),'Color',"#FD5E53");
0 comentarios
Ver también
Categorías
Más información sobre Graphics Object Properties en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!