# Pygmt plotting multiple lines

**URL:** <https://forum.generic-mapping-tools.org/t/pygmt-plotting-multiple-lines/102>\
**Category:** PyGMT Q&A\
**Created:** [November 20, 2019, 11:58am UTC](https://forum.generic-mapping-tools.org/t/pygmt-plotting-multiple-lines/102 "2019-11-20T11:58:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![huw\_g](https://avatars.discourse-cdn.com/v4/letter/h/cab0a1/32.png) [@huw\_g](https://forum.generic-mapping-tools.org/u/huw_g)\
**Post date:** [November 20, 2019, 11:58am UTC](https://forum.generic-mapping-tools.org/t/pygmt-plotting-multiple-lines/102/1 "2019-11-20T11:58:56Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![weiji14](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/weiji14/32/33_2.png) [@weiji14](https://forum.generic-mapping-tools.org/u/weiji14)\
**Post date:** [November 22, 2019, 1:24am UTC](https://forum.generic-mapping-tools.org/t/pygmt-plotting-multiple-lines/102/2 "2019-11-22T01:24:01Z")

</div>

Hi @huw_g,

The `psxy` function has been renamed to [`plot`](https://docs.generic-mapping-tools.org/latest/plot.html) in GMT6. We have a [plotting data points tutorial](https://www.pygmt.org/dev/tutorials/plot.html) in pyGMT which you can take a look at. It uses [pandas](https://pandas.pydata.org/) and you can find the documentation on PyGMT `plot` [here](https://www.pygmt.org/dev/api/generated/pygmt.Figure.plot.html). The way we usually plot lines is to pass in a list of x (longitude) and y (latitude) coordinates like so:

```python
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()

```

 ![image](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/1X/3484b110ef7876bb32f8670ffd061ffc30afb3af.png)

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.

---

<div class="post-metadata">

**Author:** ![huw\_g](https://avatars.discourse-cdn.com/v4/letter/h/cab0a1/32.png) [@huw\_g](https://forum.generic-mapping-tools.org/u/huw_g)\
**Post date:** [November 22, 2019, 10:19am UTC](https://forum.generic-mapping-tools.org/t/pygmt-plotting-multiple-lines/102/3 "2019-11-22T10:19:18Z")

</div>

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](http://gmt.soest.hawaii.edu/doc/latest/kml2gmt.html?highlight=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

---

<div class="post-metadata">

**Author:** ![weiji14](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/weiji14/32/33_2.png) [@weiji14](https://forum.generic-mapping-tools.org/u/weiji14)\
**Post date:** [November 22, 2019, 3:53pm UTC](https://forum.generic-mapping-tools.org/t/pygmt-plotting-multiple-lines/102/4 "2019-11-22T15:53:44Z")

</div>

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`](https://docs.generic-mapping-tools.org/latest/kml2gmt.html) 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](https://github.com/GenericMappingTools/pygmt/issues/382) in case anyone wants to chip in to help 👋. 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`](https://docs.python.org/3.5/library/os.html#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](http://geopandas.org/) which can read kml files (you might need to follow this [stackexchange post](https://gis.stackexchange.com/questions/114066/handling-kml-csv-with-geopandas-drivererror-unsupported-driver-ucsv) 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?

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [November 22, 2019, 6:47pm UTC](https://forum.generic-mapping-tools.org/t/pygmt-plotting-multiple-lines/102/5 "2019-11-22T18:47:06Z")

</div>

Here goes another alternative with GMT.jl (which needs a Julia install ofc) and [PyJulia](https://github.com/JuliaPy/pyjulia). Then from a python console (the GMT test kml file is from my own path, so it needs adaptation)

```auto
>>> 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)

```

 ![GMTjl_tmp](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/1X/a4e55b11869f53c0ed4f7cd8dfbefbdff7e5e682.png)
