Skip to content

Copy pygmt.Figure() instance or object to a new variable and keep the initial variable as is #3645

Open

Description

@yvonnefroehlich

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?

Related to: GenericMappingTools/agu24workshop#8 (comment), GenericMappingTools/agu24workshop#8 (comment)

import pygmt

size = 5

fig1 = 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
fig1 fig2 fig1
fig1_first fig2 fig1_second

Activity

  1. added
    question
    feature request
    discussions
    on Nov 24, 2024
  2. seisman commented on Nov 26, 2024

    @seisman
    Member

    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.

  3. added
    longterm
    and removed
    question
    on Nov 26, 2024
  4. joa-quim commented on Nov 28, 2024

    @joa-quim
    Member

    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?

  5. seisman commented on Nov 29, 2024

    @seisman
    Member

    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.

  6. joa-quim commented on Nov 29, 2024

    @joa-quim
    Member

    But if you call psconvert before gmt end the file is not yet closed and you should get the error

    psconvert [ERROR]: lixo.ps: GMT PS format detected but file is not finalized. Maybe a -K in excess? No output created.
    

    What am I missing?

  7. seisman commented on Nov 29, 2024

    @seisman
    Member

    Maybe you're not in modern mode? Try this one:

    gmt begin
      gmt figure map -
      gmt basemap -R0/10/0/10 -JX10c -Baf
      gmt psconvert -A -P -Tg -Ffig1
    
      gmt basemap -R0/5/0/5 -JX10c -Baf -X10c
      gmt psconvert -A -P -Tg -Ffig2
    gmt end
    
  8. joa-quim commented on Nov 29, 2024

    @joa-quim
    Member

    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.

  9. joa-quim commented on Nov 29, 2024

    @joa-quim
    Member

    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.

  10. seisman commented on Nov 29, 2024

    @seisman
    Member

    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.

    OK. Will give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionsfeature requestlongterm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions