GenericMappingTools / GenericMappingTools/pygmt

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

Open
#3,645 9 comments 0 reactions 0 assignees View on GitHub
discussions feature request longterm
Dominant language
Python
Stars
874
Forks
255
Avg merge
1d 21h
Merged PRs (30d)
40

Description

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: https://github.com/GenericMappingTools/agu24workshop/pull/8#issuecomment-2495692791, https://github.com/GenericMappingTools/agu24workshop/pull/8#issuecomment-2495929549

```python
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](https://github.com/user-attachments/assets/70ac7988-b907-4df9-abbe-dc0fea0ccf0b) | ![fig2](https://github.com/user-attachments/assets/480d2a5c-ddb9-4ce9-ba5d-06ca5b0a93c1) | ![fig1_second](https://github.com/user-attachments/assets/0c2ab779-bf1f-4f6f-acc9-74979e3cdc46) |

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.