PyGMT vs Plotly

Hi there,

I have to switch to Python(3) using VSCode in Interactive mode for my work and I’m having quite a hard time with figures. I’ve worked on a small exercise in which I simply plot stations.

For this I got a geojson file with

index | on/off | name | number | geometry   | area
0     |   0    |  bla |   0    | POINT(x,y) | zone_1
1     |   0    |  ble |   1    | POINT(x,y) | zone_1
128   |   1    |  bly |   64   | POINT(x,y) | zone_8

So far, the cleanest code I have to display those stations is

import plotly as px
import geopandas as gpd

data = gpd.read_file(file)
		symbol="on/off")"browser") #for the external window

It’s nice, interactive and all … But I’m not satisfied because I don’t have a map in the background.

I’ve tried a little bit to play with folium, geopandas.plot and such… but it’s convoluted and not necessarily what I’m looking for.

ANYWAY ! Back to GMT, or should I say PyGMT !

  • First question:
    Is there a way to achieve what I’ve done just above without adding too many variables (I try to avoid `x=data[“this”] ; x2 = x[“that”] ; so on …)?

  • Second question:
    Can I add a background such as CartoDB positron or OpenStreetMap ?

What I’ve done with PyGMT is :

			frame=["af", f'WSen+t"{title}"'])



But of course, it’s not as nice like this.


You can plot images as background. You’d have to find the equivalent of the plot!() command in
(the one who fills the flower)

EDIT: But on a second thought. Why no plot the image with grdimage and overlay the lines on it?

Have you considered using Cartopy? I have some simple scripts with it and it works great. I usually use it for my “working” figures and then pygmt when it’s a figure for publication.

I am only starting to try PyGMT, but I think it knows how to plot data from a GeoPandas object without extracting the variables.

When I want to make maps with OpenStreetMap background, I use QGIS.

It seems to me that some non trivial steps would be involved to co-locate points no?

Looking into it right now.

In my case, that would implies to segregate the data… that’s ok. But assigning colors manually is a little too much… no ?

x_on  = data [geometry].x & [on]  > color [area = 1]
x_off = data [geometry].x & [off] > color [area = N]

That’s an efficient point and click solution indeed, but I need to be able to script it (+ learn some python).

Hadn’t understood that you want a referenced image. Bu no problem, see grdimge -Dr

Well, I’m doing a scatter plot of stations that I want to display over a map (with roads, streets,…).

Does using grdimage -Dr mean that my static map background needs to correspond exactly to the -R domain on which I plot my stations ?

EDIT : I actually managed with plotly and this line


It means that the -R that you pass to grdimage must be that of the image, not of your data points. But next you plot them with image’s -R

Makes sense, thanks :slight_smile:
I’ll play a little more with these

Try using contextily (Introduction guide to contextily — contextily 1.1.0 documentation). They have CartoDB.positron and OpenStreetMap listed at A short look into providers objects — contextily 1.1.0 documentation.

As an aside, I’ve actually been thinking whether it’s worth integrating PyGMT with contextily so that users can get a choice of different basemaps. They have a bounds2img function that returns an RGBA numpy array, which we could georeference into an xarray.DataArray and possibly use as a basemap.

Thanks @weiji14. I’ll have a look at it.
To be honest, for simple purposes QGIS or python MAPBOX tools seem more compact to code with the bonus of having interacting vizualisation.