# Coloring ellipses based on dataframe column

**URL:** <https://forum.generic-mapping-tools.org/t/coloring-ellipses-based-on-dataframe-column/1634>\
**Category:** PyGMT Q&A\
**Created:** [May 3, 2021, 2:47am UTC](https://forum.generic-mapping-tools.org/t/coloring-ellipses-based-on-dataframe-column/1634 "2021-05-03T02:47:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@Tom](https://forum.generic-mapping-tools.org/u/Tom)\
**Post date:** [May 3, 2021, 2:47am UTC](https://forum.generic-mapping-tools.org/t/coloring-ellipses-based-on-dataframe-column/1634/1 "2021-05-03T02:47:19Z")

</div>

Hi all,

I want to color come ellipses based on a float value column (called ‘Minor’) in my dataframe (‘df380’):

```
   pygmt.makecpt(cmap="viridis", series=[df380.Minor.min(), df380.Minor.max()])
   fig.plot(data=df380, style="e", cmap=True, color=df380.Minor, pen="0p,black")

```

But this generates the following error:

` GMTInvalidInput: Can't use arrays for color if data is matrix or file.`

I can’t tell what’s going wrong here compared to other working examples online. Any help appreciated!

Tom

---

<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:** [May 10, 2021, 1:26am UTC](https://forum.generic-mapping-tools.org/t/coloring-ellipses-based-on-dataframe-column/1634/2 "2021-05-10T01:26:06Z")

</div>

Hi Tom,

Coloring of points based on a value can only be done if you input data via `x` and `y`, e.g. using something like:

```python
fig.plot(x=df380.longitude, y=df380.latitude, style="e", cmap=True, color=df380.Minor, pen="0p,black")

```

assuming that your x and y columns are named ‘longitude’ and ‘latitude’ respectively. The `GMTInvalidInput` error you are getting is because you are passing in a matrix-type pandas.DataFrame via the `data` parameter. Let us know if that helps, and if not, maybe post the output of `df380.head()` so that we can inspect a sample of your dataframe and help to reproduce your issue.

---

<div class="post-metadata">

**Author:** ![Tom](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@Tom](https://forum.generic-mapping-tools.org/u/Tom)\
**Post date:** [May 10, 2021, 1:47am UTC](https://forum.generic-mapping-tools.org/t/coloring-ellipses-based-on-dataframe-column/1634/3 "2021-05-10T01:47:03Z")

</div>

Thanks for the suggestion, but that actually produces a blank plot (no error, just a blank figure). I guess that’s because there is no input to say what the direction + minor and major axes of the ellipses are, whereas simply stating `data = df` will automatically find those parameters for you, so long as your dataframe is in the right format.

For reference, here is the output of `df380.head()`:

| | Longitude | Latitude | azimuth | Major | Minor |
| --- | --- | --- | --- | --- | --- |
| 1 | 143.890395 | -39.836134 | 86.690006 | 0.991056 | 0.915180 |
| 2 | 143.631681 | -38.845903 | -88.008732 | 0.994992 | 0.957652 |
| 3 | 144.163615 | -39.269567 | 89.187011 | 0.986036 | 0.921038 |
| 4 | 144.432449 | -38.702371 | -78.331177 | 0.982666 | 0.909278 |
| 5 | 144.697050 | -38.134570 | -57.902771 | 1.017184 | 0.806435 |

---

<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:** [May 10, 2021, 3:25am UTC](https://forum.generic-mapping-tools.org/t/coloring-ellipses-based-on-dataframe-column/1634/4 "2021-05-10T03:25:19Z")

</div>

Ah right, ellipses, should have read more closely! In this case, you might want to try the new [pygmt.Figure.velo](https://www.pygmt.org/dev/api/generated/pygmt.Figure.velo.html) function. It’s not available in the stable PyGMT v0.3.1 release, so you’ll need to [install the PyGMT development version](https://www.pygmt.org/dev/install.html#using-pip) to use it:

```auto
pip install --pre --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple pygmt

```

after that, you should be able to use `fig.velo`. The specification (spec) is a bit hardcoded, so you might need to reorder your pandas.DataFrame columns to be in the right order ( **1** , **2** : longitude, latitude **3** , **4** : eastward, northward velocity **5** , **6** : semi-major, semi-minor axes). I had to make up some data for the Eastward/Northward columns to get the example below to work.

```python
df380 = pd.DataFrame(
    data={
        "Longitude": [143.890395, 143.631681, 144.163615, 144.432449, 144.697050],
        "Latitude": [-39.836134, -38.845903, -39.269567, -38.702371, -38.134570],
        "Eastward": [0.5, 0.5, 0.5, 0.5, 0.5], # change this made up data
        "Northward": [0.75, 0.75, 0.75, 0.75, 0.75], # change this made up data
        "Major": [0.991056, 0.994992, 0.986036, 0.982666, 1.017184],
        "Minor": [0.915180, 0.957652, 0.921038, 0.909278, 0.806435],
        "azimuth": [86.690006, -88.008732, 89.187011, -78.331177, -57.902771],
    }
)

fig = pygmt.Figure()
fig.velo(
    data=df380,
    spec="r0.5/0.95",
    pen="0.6p,red",
    uncertaintycolor="lightblue1",
    frame=True,
)
fig.savefig(fname="ellipses.png")
fig.show()

```

which produces:

 ![ellipses](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/0/0e6a1ce2cb82dd52a462086e124b0307ad2a903b.png)

Hopefully the code above will be enough to get you started. Let us know if you need any clarification, it would be great to get feedback on the `velo` function actually before we release it officially in PyGMT v0.4.0 (sometime in June 2021 perhaps).

References:

- [Velocity arrows and confidence ellipses — PyGMT](https://www.pygmt.org/dev/gallery/seismology/velo_arrow_ellipse.html)
- [velo — GMT 6.6.0 documentation](https://docs.generic-mapping-tools.org/latest/supplements/geodesy/velo.html)

---

<div class="post-metadata">

**Author:** ![Tom](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@Tom](https://forum.generic-mapping-tools.org/u/Tom)\
**Post date:** [May 10, 2021, 3:37am UTC](https://forum.generic-mapping-tools.org/t/coloring-ellipses-based-on-dataframe-column/1634/5 "2021-05-10T03:37:24Z")

</div>

Ahhh that looks promising. To be honest, in the meantime since asking this question I devised a solution that worked for my purposes in QGIS, but next time I am doing similar work I will try this.

Thank you for the wonderful help!
