-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange behaviour when using Session.call_module(). #1582
Comments
Hi @timrlaw, thanks for the detailed bug report! I haven't had time to fully reproduce this, but just to try and answer some of your questions quickly:
Have you tried using
Having the colorbar is positioned relative to the inset map outside the |
Hi @weiji14 Sure thing, here are some pictures of how it looks. Starting from the code sample I put above, the image is "as desired": If I move the colorbar call to be after the inset:
If change "BL" on the inset to "TL", then I get this (certainly not in the top-left): If I move I tried using "M?" on the inset I would also note that if I manually set |
Ok, I figured it out. You need to put your region = [-51, -48, -24, -23]
inset_region = [-80, -28, -43, 0]
inset_w = 2
inset_h = mapproject_height(region="80W/28W/43S/0S", projection="M2c") # put this before starting a new Figure!!
fig = pygmt.Figure()
grid = pygmt.datasets.load_earth_relief(resolution="03s", region=region)
fig.grdimage(grid=grid, projection="M15c", frame=True, cmap="world")
fig.colorbar(frame=["x", "y+lm"], position="JBC+o0c/0.6c+w4.8c")
with fig.inset(position=f"jTL+w{inset_w}c/{inset_h}c+o0.1c", box="+pblack"):
fig.coast(
region=inset_region,
projection="M?",
land="gray",
shorelines="thin",
water="white",
)
fig.savefig("test.png") produces With this, you should be able to put the This does seem similar to the global/local session problem @seisman mentioned in #733 (comment). What I don't get is that |
|
@weiji14, that code gives me the same result as my fourth picture above. I have tried with the version of PyGMT in PyPi and building from source. Are you perhaps using a development version of GMT? Could you please paste your @meghanrjones, that makes some sense. If I put a second This is quite confusing as a user, how do I know which PyGMT functions mutate the internal GMT state and which do not? Maybe I can make this work by scattering extra I have never used regular GMT, only PyGMT, so I have no mental model for how GMT works. However the API for PyGMT gives the impression that there is no internal state---based on how matplotlib and similar libraries work I would not expect calls made prior to Thanks both for your help. |
Thanks for all your detailed reports. It is always helpful to get user feedback and non-GMT users provide an important perspective. The improper setting of the inset dimensions may be an upstream bug; you shouldn't need to use mapproject to get this to work. I have opened an issue at GenericMappingTools/gmt#5875 and will post back with the relevant results for PyGMT. Regarding the state changes, I agree that calls before creating a figure instance should not affect the plot. |
Independent of the issues you're having with the region setting, using mapproject is the correct way to get the inset size to agree with the region and projection. We'll look into simplifying this upstream. |
Thanks @meghanrjones. For reference, my use case is automatically generating plots to be displayed on a website. Hence why I cannot manually fiddle the inset height---it needs to be fully programmable. |
Really sorry for the confusion @timrlaw, I posted the wrong code 😅, forgot to put a region = [-51, -48, -24, -23]
inset_region = [-80, -28, -43, 0]
inset_w = 2
inset_h = mapproject_height(region="80W/28W/43S/0S", projection="M2c") # put this before starting a new Figure!!
fig = pygmt.Figure()
grid = pygmt.datasets.load_earth_relief(resolution="03s", region=region)
fig.grdimage(grid=grid, region=region, projection="M15c", frame=True, cmap="world") # need to reset the region here
fig.colorbar(frame=["x", "y+lm"], position="JBC+o0c/0.6c+w4.8c")
with fig.inset(position=f"jTL+w{inset_w}c/{inset_h}c+o0.1c", box="+pblack"):
fig.coast(
region=inset_region,
projection="M?",
land="gray",
shorelines="thin",
water="white",
)
fig.savefig("test.png") For reference, this is my
Yes this is good feedback, and is essentially what we have been discussing at #1282 on having unintended behaviour arising from using a single GMT session for everything in PyGMT. It is a tricky problem which Leo appears to have debated on early in PyGMT's history (see #327 (comment)). @meghanrjones, do you think it would be better to let Lines 47 to 56 in 65995f6
|
Thanks @weiji14, that works a treat, I think I can do everything I need now. As long as I do all my I will let you decide whether or not to close this issue now or keep it around pending possible changes. Thanks again both for your help. |
I like your idea of providing an option for In the meantime, this seems to be a recurring tricky point with the history of parameters when dealing with figures, subplots, and insets. I think we should add some documentation to clarify these points and will open a separate issue to track that. |
Description of the problem
Hi all. I'm having issues related to sessions, and the order in which seemingly unrelated commands are issued affecting the final plot in strange ways. I think this is related to #1242 and #733.
Essentially what I'm trying to do is set the dimensions of an inset figure to match the dimensions of the inset map. Perhaps there is a better way to do this, but the way I've found is to use GMT's
mapproject
to calculate the height given the width. PyGMT doesn't exposemapproject
so I have to useSession.call_module()
, see the code below.The problem is that doing this results in strange behaviour, dependent on where exactly the
mapproject
call is issued. I have found a way to arrange the code to get the figure I want, but it is very fragile, and the inset options do not work correctly.Full code that generated the error
This is the "working" sample:
Issues arise when trying to:
with
statement.mapproject_height
call to the beginning, causes the outer figure to use the region boundaries from the inset for some reason.Basically this only works if the inset is in the bottom-left, and plotted last, and the
mapproject_height
call is issued right before thewith
statement. This causes problems when I want to make more complex plots.Full error message
System information
The text was updated successfully, but these errors were encountered: