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
pygmt does not display newly created figures using show().
In [4]: fig = pygmt.Figure()
...: fig.coast(shorelines=True, region=[-90, -70, 0, 20], projection="M8i", frame=True)
...: fig.show()
pygmt-session [ERROR]: GMT_COMPATIBILITY: Expects values from 6 to 6; reset to 6.
pygmt-session [ERROR]: GMT_COMPATIBILITY: Expects values from 6 to 6; reset to 6.
pygmt-session [ERROR]: GMT_COMPATIBILITY: Expects values from 6 to 6; reset to 6.
pygmt-session [ERROR]: GMT_COMPATIBILITY: Expects values from 6 to 6; reset to 6.
pygmt-session [ERROR]: GMT_COMPATIBILITY: Expects values from 6 to 6; reset to 6.
Out[4]: <IPython.core.display.Image object>
Hi @MarkWieczorek, I'm not sure if there's a way to put pygmt figures inside matplotlib (point 2), but in regard to a matplolib compatible syntax (point 1), @leouieda definitely put some thought before on how to make the display mechanisms better at #269.
If it's subplots you're interested in, the modern mode in GMT 6 does have a subplot feature, but that's not wrapped yet. I actually have some local code to implement that but it'll take time to get into PyGMT properly.
Could you describe in a bit more detail what you're trying to plot? If I'm reading this correctly, are you interested in showing the figure inline inside of an IPython terminal?
Right, so you want to make a plot_gmt function that has a show flag which will automatically display the figure when called! I can see where you're going here (I've written a similar thing before with matplotlib). So yes, the problem is that PyGMT's fig.show() doesn't quite work that way (yet?)...
@MarkWieczorek I started a branch for implementing #269 (PR coming soon) and here is what I have working:
Calling Figure.show() will open an external viewer and return something that Jupyter knows how to embed in the notebook. There is no need for method="enternal".
Calling pygmt.enable_notebook() will disable the external viewer. This can also be set with an environment variable. I don't really like this but I don't think there is another way that is always reliable. matplotlib does this by deep integration with Jupyter (i.e., jupyter has code to handle matplotlib figures) which I don't think we can mirror yet.
So you could have fig = grid.plot_gmt(projection='...', ..., show=True) and things should work the way you expect.
Regarding inserting plots in matplotlib, the problem is that the figure would be a raster. We could get a PNG returned by PyGMT and insert that as matplotlib axis. The problem is that it looks bad when exporting the figure later on. This was my original idea for the display mechanism but I gave up on it at the time. The "best" way to do this would be to translate postscript to matplotlib's vector format but I doubt anyone would be willing to do it. I'm not even sure it's possible since GMT does a lot of coding in the postscript itself.
If anyone is can find a way of inserting raster into matplotlib in a way that it looks good, I'd be happy to implement this in PyGMT. Mixing GMT and matplotlib in the same figure would be a dream!
Activity
MarkWieczorek commentedon Nov 4, 2019
I note that this does in fact work in a jupyter notebook, but not in a standard terminal session.
seisman commentedon Nov 4, 2019
In an IPython terminal, you need to call
fig.show(method="external")to display the figure.MarkWieczorek commentedon Nov 4, 2019
That appears to save the figure as a temporary file, and then opens it with an external application.
Would it be possible to
What I would really like to do is have a simple way of choosing to plot something using either standard matplotlib or gmt, such as:
weiji14 commentedon Nov 5, 2019
Hi @MarkWieczorek, I'm not sure if there's a way to put pygmt figures inside matplotlib (point 2), but in regard to a matplolib compatible syntax (point 1), @leouieda definitely put some thought before on how to make the display mechanisms better at #269.
If it's subplots you're interested in, the modern mode in GMT 6 does have a subplot feature, but that's not wrapped yet. I actually have some local code to implement that but it'll take time to get into PyGMT properly.
Could you describe in a bit more detail what you're trying to plot? If I'm reading this correctly, are you interested in showing the figure inline inside of an IPython terminal?
MarkWieczorek commentedon Nov 5, 2019
What I would like to do is create a custom plotting method for use with pyshtools. The idea would be to take an instance of a grid and then do
The
show=Truewould show the image if you are in a terminal or in a notebook.You seem to be saying that this is impossible, so the alternative would be to return a figure, and then manually specify to show it, like
weiji14 commentedon Nov 5, 2019
Right, so you want to make a
plot_gmtfunction that has ashowflag which will automatically display the figure when called! I can see where you're going here (I've written a similar thing before with matplotlib). So yes, the problem is that PyGMT'sfig.show()doesn't quite work that way (yet?)...Could you point me to some sample code on what that
plot_function looks like (a non-gmt one will suffice). I can see you've opened a GMT milestone at https://github.com/SHTOOLS/SHTOOLS/milestone/1leouieda commentedon Nov 28, 2019
@MarkWieczorek I started a branch for implementing #269 (PR coming soon) and here is what I have working:
Figure.show()will open an external viewer and return something that Jupyter knows how to embed in the notebook. There is no need formethod="enternal".pygmt.enable_notebook()will disable the external viewer. This can also be set with an environment variable. I don't really like this but I don't think there is another way that is always reliable. matplotlib does this by deep integration with Jupyter (i.e., jupyter has code to handle matplotlib figures) which I don't think we can mirror yet.So you could have
fig = grid.plot_gmt(projection='...', ..., show=True)and things should work the way you expect.Regarding inserting plots in matplotlib, the problem is that the figure would be a raster. We could get a PNG returned by PyGMT and insert that as matplotlib axis. The problem is that it looks bad when exporting the figure later on. This was my original idea for the display mechanism but I gave up on it at the time. The "best" way to do this would be to translate postscript to matplotlib's vector format but I doubt anyone would be willing to do it. I'm not even sure it's possible since GMT does a lot of coding in the postscript itself.
If anyone is can find a way of inserting raster into matplotlib in a way that it looks good, I'd be happy to implement this in PyGMT. Mixing GMT and matplotlib in the same figure would be a dream!