Pygmt plotting multiple lines

Hi,

I have previously been using GMT 4 (linux) and I am converting to pyGMT with GMT 6 (windows).

I am trying to plot a series of lines composed of lat-long coordinates, and the file layout is the output of kml2gmt in the format shown below:

kml2gmt: KML read from faults_at_depth.kml

Faults at Depth.kmz

-L"Line1"
15.2467712458 40.5677764864
15.2653163928 40.5613102162
15.2892336577 40.5523798281
15.2943839775 40.5506614258
-L"Line2"
15.114091938 40.8637241151
15.1141461644 40.8637064216
15.1150913548 40.8633980347

etc…

In GMT4 I plot this using:
psxy $faults_ad $proj $bounds -W30/red -m -O -V -K >> $psfile

If someone could point me towards an example of this sort of data being plotted, or explain the best way to plot it that would be great!

thanks!

Hi @huw_g,

The psxy function has been renamed to plot in GMT6. We have a plotting data points tutorial in pyGMT which you can take a look at. It uses pandas and you can find the documentation on PyGMT plot here. The way we usually plot lines is to pass in a list of x (longitude) and y (latitude) coordinates like so:

import pygmt

fig = pygmt.Figure()
fig.plot(
    x=[40.5677764864, 40.5613102162, 40.5523798281, 40.5506614258],  # longitude
    y=[15.2467712458, 15.2653163928, 15.2892336577, 15.2943839775],  # latitude
    projection="M10c",  # projection is an alias for '-J'
    region=[40.54, 40.58, 15.24, 15.30],  # region is an alias for '-R'
    pen="30/red",  # pen is an alias for '-W'
    frame="af",  # frame is a alias for '-B'
)
fig.show()

The fig.plot function has a data argument which should accept a filename (instead of having to use x, y coordinates), but I’m not too sure if your kml file will work out of the box (maybe post a sample so we can take a look?). Let us know if you have any problems and we’ll try to help.

1 Like

Hi @weiji14,

Thank you for your quick response.The problem with passing x and y coordinates in as in your example is that there are multiple different lines contained in one file. I could not find a way to plot the two separate lines in one command using the pandas method, though I am relatively new to python/pandas.

Solution
The suggestions of using fig.plot data argument worked well on the file format shown in my first post, thanks!
I used:

fig.plot(data = ‘file_of_lines.txt’)

where ‘file_of_lines.txt’ is in the format:

-L"Line1"
15.2467712458 40.5677764864
15.2653163928 40.5613102162
15.2892336577 40.5523798281
15.2943839775 40.5506614258
-L"Line2"
15.114091938 40.8637241151
15.1141461644 40.8637064216
15.1150913548 40.8633980347.

I generated ‘file_of_lines.txt’ using kml2gmt from a kml file which contained 2 different sets of points delineating two lines. I generated this in normal gmt (non-pygmt). Is it possible to run kml2gmt within pygmt?

As an aside I tried to use the same command on a kml file:

fig.plot(data = ‘file_of_lines.kml’)

This produced the error

plot[ERROR]: Mismatch between actual (1) and expected (2) fields near line 3 in file
plot [ERROR]: Mismatch between actual (1) and expected (2) fields near line 4 in file file_of_lines.kml

which may be of interest to you

Cool, good to know that passing in the .txt file works! Multiple lines are a bit harder to plot using pandas/pygmt (you might need to write a for-loop) so maybe forget about that method for now.

The kml2gmt function isn’t implemented yet in pygmt, but I’ve opened up a feature request on Github for implementing it at Wrapper for kml2gmt · Issue #382 · GenericMappingTools/pygmt · GitHub in case anyone wants to chip in to help :wave:. In the meantime, if you’re using a jupyter notebook, you can run bash commands directly using an exclamation mark (e.g. !kml2gmt faults_at_depth.kml file_of_lines.txt). Otherwise use os.system or something along those lines.

This might not directly work for your case of plotting multiple lines, but for anyone else looking in the future, there is a Python package called geopandas which can read kml files (you might need to follow this stackexchange post to enable the kml driver though) which should make it possible to use fig.plot in pygmt. I’ve not actually tried this myself though. Just wondering if plain GMT actually plots kml files directly?

Here goes another alternative with GMT.jl (which needs a Julia install ofc) and PyJulia. Then from a python console (the GMT test kml file is from my own path, so it needs adaptation)

>>> from julia import GMT
>>> K = GMT.kml2gmt("C:/progs_cygw/GMTdev/gmt5/master/test/kml/lines.kml")
>>> GMT.lines(K, limits=[-0.5,1.5,-0.5,1.5], fmt="png", show=True)