You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Copy pygmt.Figure() instance or object to a new variable and keep the initial variable as is #3645
Copying a pygmt.Figure() instance or object to a new variable can be useful, e.g., within a Jupyter notebook for re-running cells with different arguments passed to the parameters to test different visualizations. However, this seems to be not straight forward. In the example below, I would like to achieve that fig1 remains as is after adding features to fig2. Neither running fig2 = fig1 nor running fig2 = copy.deepcopy(fig1) before changing fig2 works as expected. Maybe there is a relationship to continuing the GMT session and not creating a new one?
importpygmtsize=5fig1=pygmt.Figure()
fig1.basemap(projection="X5c", region=[-5, 5, -5, 5], frame=True)
fig1.show()
fig2=fig1# OR (both does not work as expected)# fig2 = copy.deepcopy(fig1)fig2.plot(x=0, y=0, style="c1c")
fig2.show()
fig1.show() # Now fig1 also contains the circle and is identical to fig2
This feature is difficult to implement. The main reason is that, GMT stores the half-baked PS file, gmt.history, gmt.conf and even inset/subplot settings in temporary files. To implement what you expect with the syntax like fig1 = fig2.copy(), we need to copy fig1-related files and make necessary changes to ensure it works for fig2, which means we have to know all the details about GMT and even have to use GMT's low-level, private functions.
Yes, modern mode is quite tricky and does not lets us almost no liberty to inovate. However, I am a bit confused with this behavior, but for another reason.
When you call fig1.show() shouldn't it close the PS file and delete the temporary sessions dir? How come that you can still append to fig1 if, besides surviving to show(), it had to be closed order to be displayed?
When you call fig1.show() shouldn't it close the PS file and delete the temporary sessions dir? How come that you can still append to fig1 if, besides surviving to show(), it had to be closed order to be displayed?
We're doing this in PyGMT:
gmt begin
gmt figure figname - # Need to pass '-' as a special format
gmt basemap ...
gmt coast ...
gmt psconvert ...
open figname.png
gmt grdimage ...
gmt psconvert ...
open figname.png
gmt end
When calling Figure.show, we call psconvert to convert the PS file into the desired format and then open it in an external viewer. The PS file is still there until we call gmt end. That's why we can append more layers even after calling Figure.show.
Hmm, I see. I guess I had forgotten or didn't even know that we could do that in modern mode. I barely use it, except for the insets in Julia where fish inside some of the tmp files to learn the coordinates where to put the inset. It's a crazy mixed scheme between modern and classic.
Ah, so it was a forgotten thing since psconvert.c is one of the codes that I visit more often.
And speaking of which, it would be nice if you could use the psconvert-no-file-dup branch (the one where psconvert edits the ps file directly instead of making a copy) in your PyGMT tests. I've been using it in Julia and found no problems so far.
And speaking of which, it would be nice if you could use the psconvert-no-file-dup branch (the one where psconvert edits the ps file directly instead of making a copy) in your PyGMT tests. I've been using it in Julia and found no problems so far.
Activity
seisman commentedon Nov 26, 2024
This feature is difficult to implement. The main reason is that, GMT stores the half-baked PS file, gmt.history, gmt.conf and even inset/subplot settings in temporary files. To implement what you expect with the syntax like
fig1 = fig2.copy(), we need to copyfig1-related files and make necessary changes to ensure it works forfig2, which means we have to know all the details about GMT and even have to use GMT's low-level, private functions.joa-quim commentedon Nov 28, 2024
Yes, modern mode is quite tricky and does not lets us almost no liberty to inovate. However, I am a bit confused with this behavior, but for another reason.
When you call
fig1.show()shouldn't it close the PS file and delete the temporary sessions dir? How come that you can still append tofig1if, besides surviving toshow(), it had to be closed order to be displayed?seisman commentedon Nov 29, 2024
We're doing this in PyGMT:
When calling
Figure.show, we callpsconvertto convert the PS file into the desired format and then open it in an external viewer. The PS file is still there until we callgmt end. That's why we can append more layers even after callingFigure.show.joa-quim commentedon Nov 29, 2024
But if you call
psconvertbeforegmt endthe file is not yet closed and you should get the errorWhat am I missing?
seisman commentedon Nov 29, 2024
Maybe you're not in modern mode? Try this one:
joa-quim commentedon Nov 29, 2024
Hmm, I see. I guess I had forgotten or didn't even know that we could do that in modern mode. I barely use it, except for the insets in Julia where fish inside some of the tmp files to learn the coordinates where to put the inset. It's a crazy mixed scheme between modern and classic.
seisman commentedon Nov 29, 2024
Related codes are at https://github.com/GenericMappingTools/gmt/blob/39cff6e579e7e5e1b17692122ce15e98e6da109b/src/psconvert.c#L1818.
joa-quim commentedon Nov 29, 2024
Ah, so it was a forgotten thing since psconvert.c is one of the codes that I visit more often.
And speaking of which, it would be nice if you could use the
psconvert-no-file-dupbranch (the one where psconvert edits the ps file directly instead of making a copy) in your PyGMT tests. I've been using it in Julia and found no problems so far.seisman commentedon Nov 29, 2024
OK. Will give it a try.