How to change color from a .fig file having subplots

4 次查看(过去 30 天)
I'm creating a function which takes as input a .fig file and it modifies its properties following a standard that I defined.
I would like to add an option to change the color of the plots according to a color order that I defined. If the figure has only one plot it's easy, I can modify the color property of each Line found with findobj, but I'm stuck with the figures having subplots.
I was thinking to use the ColorOrder option taken from the axes properties but the figure does not 'refresh' with the new colors (I tried also to use refresh( ... ) ).
Another idea I had was to modify the color Line by Line but I'm not able to distinguish to which subplot they belong (in theory every subplot should restart the color order).
Here is an idea of the outcome where on the left there is subplot(121) not yet modified while in subplot(122) there is the plot with the new color order. At the end subplot(121) and subplot(122) should have the same colors.
  3 个评论
Emanuele Rondanina
Emanuele Rondanina 2017-8-31
Your comment is correct, I didn't know I could have the lines data starting from the axes handles. I though the axes handles had only properties strictly related to the axes while to get the lines I had to use findobj.
Adam
Adam 2017-8-31
编辑:Adam 2017-8-31
The axes properties do strictly relate to the axes, but since one of those is 'Children', this contains the objects that are parented by the axes (whether they be lines, images, scatter plots, etc) so they when you retrieve them they can be interrogated themselves.
doc inspect
is an excellent tool for playing around with graphics objects and seeing more visually what you can access and how changing them affects the figure.
e.g.
inspect( hAxes )
will show you the axes properties in a dialog where they are easily editable. The end effect is no different to changing them programatically, but it is often faster when you just want to look around at what can be changed and what its effect is.

请先登录,再进行评论。

采纳的回答

José-Luis
José-Luis 2017-8-31
编辑:José-Luis 2017-8-31
Creating figure and forgetting all handles:
for ii = 1:2
subplot(1,2,ii);
plot(1:10,sort(rand(10)));
end
savefig('dummy')
close all
Loading figure:
fH = openfig('dummy.fig');
colorArray = eye(3);
numColors = size(colorArray,1);
%Getting axes objects
aH = findall(fH,'Type','axes').';
for ii = aH
lH = findall(ii,'Type','line').';
cnt = 0;
for jj = lH
idx = mod(cnt,numColors) + 1;
jj.Color = colorArray(idx,:);
jj.LineWidth = 2;
cnt = cnt + 1;
end
end
Your mileage may vary. Off the top of my head, it will not work if there are lines that are not visible. You'd need to adapt for that.
If you are not loading a figure but generating it, keeping track of your handles is the sane solution.
  1 个评论
Emanuele Rondanina
Emanuele Rondanina 2017-8-31
Thanks for your answer, this is the outcome I was searching for. I didn't know you could extract line type of data from axes properties.
The function I'm creating is intended to modify report plots. Let's say the editor is asking me a different color scheme or font I prefer to load the figures instead of modifying the post processing which generates them. In another case I agree with you the sane solution is saving the handles!
Thanks again for your help!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by