piermorel/gramm

copying object to axes

kenziew opened this issue · 5 comments

First, this is awesome! Second, Is there any way to plot to a certain axes? I'm working in app designer and am trying to find a way to display the plot in the app axes but am not having luck with imhandles, copying the image, copyobj etc

Hi @kenziew, thanks for the feedback ! Normally you should be able to get the handles of the gramm axes through the gramm object itself after gramm_object.draw() is called. They are in the property gramm_object.facet_axes_handles. Does that allow you to do what you want ?

This is an old Issue but figured I'd check-in to see if you have advice/solutions since I think I want to do a similar thing described here and in #23 .

Basically, I have drafted individual sub-figures (each sub-figure is a distinct gramm object with it's own faceting etc.) and now I want to simply "plug them in" to a new single figure without copying too much of the code.

As a simple example: I have a gramm object for Fig 1a drawn in a figure, and another gramm object for Fig 1b drawn in a distinct figure. I want to create a single figure and assign the contents of Fig 1a to the first row of subplots and the contents of Fig 1b to the second row of subplots.

I have tried using copy() as described in #23 to create a new gramm object containing both Fig 1a and Fig 1b but as that author of that Issue mentions, it seems that the handle references of gramm objects are tied to their figures so when I try to draw() my new gramm object the contents are drawn on distinct figures.

Simpllified example code and output of what I’m trying to do follows:


%% What I’d like to do is make fig1a, fig2b separately then simply combine them into a new Figure as subplots

%- make figure 1a gramm object

figure;

fig1a= gramm('x',data.x,'y',data.y, 'color', data.color);

fig1a.facet_grid([],data.label); %facet results in 5 subplotted columns

fig1a.stat_summary('type','sem','geom','line');

%- copy this into Figure1 gramm object prior to drawing

fig1grand(1,1)= copy(fig1a); %assign fig1a to row 1 of the new Figure

fig1a.draw();

%- make figure 1b gramm object 

figure;

fig1b= gramm('x',data2.x2,'y',data2.y2, 'color', data2.color);

fig1b.facet_grid([],data.label); %facet results in 5 subplotted columns

fig1b.stat_summary('type','sem','geom',{'bar', 'black_errorbar'}, 'dodge', dodge, 'width', width);

%- copy this into Figure1 gramm object prior to drawing

fig1grand(2,1)= copy(fig1b); %assign fig1b to row 2 of the new Figure

fig1b.draw();

%- try drawing the combined contents in a new Figure, but it still draws in separate figures (maybe that I am copying fig1a and fig1b gramm object handles that are tied to their figures?)

figure;

fig1grand.draw();

The output is 3 figures. The Figure 3 (what I would expect to be combined 1+2) is blank. Interestingly the content and relative positions of Figure 1 and Figure 2 are what I’d like (Figure 1 is fig1a placed into row 1; Figure 2 is fig1b placed into row 2), but they aren’t being combined in a single figure. Appreciate any suggestions!

output_figure1
output_figure2
output_figure3_blank

Hi Dakota, I can’t test things currently, but have you tried without calling the draw() function on the initial fig1a and fig1b objects ? I’m not sure how copy would interfere with that, but when drawing array of gramm objects a single draw call should be made on the whole array only.

Hi Pierre,

Thanks for the prompt reply! I was able to get this to work by simply using the same MATLAB figure for all the gramm objects. In the old code above I use figure() to make a new figure before making each gramm object. If I just remove those figure() calls I get a single figure with my expected output (attached here).

output_single_figure

Now I'm working on adding some additional subplots with different faceting and getting them all aligned neatly (specifically a single graph in position fig1(3,1) and another in position fig1(3,2). I'm having some issues at this point, and am trying some stuff mentioned in #40 and #94 so when I get it worked out I can share my solution.

output_with_2c_and_2d

Now I'm working on adding some additional subplots with different faceting and getting them all aligned neatly (specifically a single graph in position fig1(3,1) and another in position fig1(3,2). I'm having some issues at this point, and am trying some stuff mentioned in #40 and #94 so when I get it worked out I can share my solution.

output_with_2c_and_2d

Not sure where the best place to post this info is at this point (I’ve mentioned a few different Issues throughout and can make my own if that makes sense?) but wanted to share my solution in case it helps others:

I think the copy() approach should work fine if you have similar faceting between plots (in my prior post, you can see that overall the figure layout looks good with just sub-figures 1 & 2, it just needs some text size adjustment and cleaning up).

With this particular Figure though, once I tried to add sub-figures 3 & 4 I was trying to combine multiple gramm objects with different faceting. I ran into some issues just trying to copy() them. Though the gramm objects were drawing in the relatively correct spot, I couldn’t seem to get the layout neat.

So, I decided to scrap that approach and use UIpanels as described in #40. This actually works really well (see final output fig) and I’d recommend others to just do this. Despite Pierre’s comment there that dynamic resizing is broken, I have found everything seems to be behaving very well for me. It requires pretty minimal editing of my existing code, just need to tweak the UIpanels to get a layout you like and set_parent() prior to the first draw call of each gramm object. This is the approach I plan to use for the rest of my Figures.

@piermorel. thanks for making gramm, sharing it with us, and for taking the time to check-in on my comment! Gramm remains a great tool for anyone using MATLAB!

final_output_uiPanels