GenericMappingTools / pygmt

A Python interface for the Generic Mapping Tools.
https://www.pygmt.org
BSD 3-Clause "New" or "Revised" License
758 stars 220 forks source link

Problem coloring symbols in pygmt v0.11 with plot and plot3d commands #3219

Closed WilliamEpenarandaA closed 6 months ago

WilliamEpenarandaA commented 6 months ago

Description of the problem

Greetings,

I'm having troubles filling symbols with the colors based on a different variable using PyGMT v0.11 and GMT 6.3 plot3d commands in Ubuntu 18.04 OS. This is the same issue reported in this post https://forum.generic-mapping-tools.org/t/coloring-symbols-with-cpt-pygmt/4034. I have seen other comments, but none of them seem to work for my case.

I test the example 3d scatter plot found in the pygmt webpage and works normally. The problem appeared when I try to plot in 3d view some earthquakes and colored according to different depth values. The data is saved into a pandas dataframe, data_dev that have this structure:

Index Date Hour_UTC Latitude Longitude SGC_depth Mw_x Lat-cent Lon-cent Mt_depth Mw_y Pdc Pclvd Piso Variance VarRed
0 2015-09-07 17:35:31 6.815 -73.145 150.10 4.4 6.815 -73.055 135 6.03 85 15 0 6.866000e-08 54.48
1 2013-12-30 00:12:58 6.811 -73.146 152.00 4.5 6.947 -73.146 155 4.54 96 4 0 3.432000e-11 56.96
2 2018-01-21 17:51:59 6.837 -73.139 147.70 4.6 6.656 -72.958 195 5.35 59 41 0 1.373000e-08 42.88
3 2014-03-30 08:36:59 6.796 -73.155 142.20 4.6 6.977 -73.336 160 4.96 56 44 0 1.724000e-10 72.48
4 2014-11-20 05:40:10 6.817 -73.165 153.60 4.7 6.998 -73.029 135 4.51 61 39 0 1.382000e-11 88.70
5 2016-02-11 07:14:37 6.818 -73.135 154.20 4.7 6.637 -72.999 146 4.49 56 44 0 3.606000e-11 62.59
6 2017-02-01 00:18:26 6.773 -73.164 149.00 4.7 6.773 -73.345 115 4.31 84 16 0 1.469000e-11 67.57
7 2018-02-04 15:11:17 6.823 -73.159 152.60 4.7 6.642 -72.978 125 5.38 68 32 0 7.063000e-09 72.24
8 2016-06-23 22:28:56 6.815 -73.162 149.10 4.7 6.679 -72.981 135 5.81 63 37 0 2.076000e-08 48.97

Below is the code than I'm using

#%% Import the require modulus
import pandas as pd
import pygmt
import matplotlib.pyplot as plt
import os

## Loading the data needed
file = 'MT-dev_inv-results.xlsx'
basepath = os.path.split(os.path.dirname(__file__))[0]
filepath = os.path.join(basepath, file)
cols2_use = ['Date', 'Hour_UTC', 'Latitude', 'Longitude', 'SGC_depth', 'Lat-cent', 'Lon-cent', 'Mt_depth', 'Variance',\
    'VarRed', 'Pdc', 'Pclvd', 'Piso', 'Mw_x', 'Mw_y']

data_dev = pd.read_excel(filepath, usecols=cols2_use)

#%% Drawing the 3d perspective view
region=pygmt.info(data=data_dev[['Longitude', 'Latitude', 'SGC_depth']], 
    per_column=True, spacing=(0.2, 0.2, 50))

fig = pygmt.Figure()

pygmt.makecpt(cmap="viridis", 
              series=(100, 200, 1), 
              reverse=None, 
              continuous=None,
)
fig.plot3d(x=data_dev['Longitude'], y=data_dev['Latitude'], z=data_dev['SGC_depth'],\
cmap=True, style='u0.15c', region=region, size=data_dev['Mw_x']*1.2,
perspective=[240, 30], zscale=-0.05
)
fig.plot3d(x=data_dev['Lon-cent'], y=data_dev['Lat-cent'], z=data_dev['Mt_depth'],\
cmap=True, style='u0.3c', region=region, size=data_dev['Mw_y']*1.2, \
perspective=[240, 30], zscale=-0.05, frame=["WSneZ", "xa0.2f0.1g0.1+lLongitude [°]", "ya0.2f0.1g0.1+lLatitude [°]", "za50f20g20+lDepth [km]"]
)
fig.colorbar(frame=["a20f10", "y+lkm"], position="JRM")
fig.show()

The figure that it creates is the following and in the console doesn't appear any error or warning imagen The code runs, but all the cubes plot black instead of using the color palette that I assigned in makecpt. Is there any problem or error in the code wrote to plot the image.

Thank in advance for your help!!!

PD: I attach the output to the command pygmt.show_versions() below:

PyGMT information: version: v0.11.0 System information: python: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:38:13) [GCC 12.3.0] executable: /home/william/anaconda3/envs/pygmt/bin/python machine: Linux-5.4.0-150-generic-x86_64-with-glibc2.27 Dependency information: numpy: 1.26.4 pandas: 2.2.2 xarray: 2024.3.0 netCDF4: 1.6.5 packaging: 24.0 contextily: None geopandas: None ipython: None rioxarray: None ghostscript: 10.03.0 GMT library information: binary version: 6.5.0 cores: 12 grid layout: rows image layout: library path: /home/william/anaconda3/envs/pygmt/lib/libgmt.so padding: 2 plugin dir: /home/william/anaconda3/envs/pygmt/lib/gmt/plugins share dir: /home/william/anaconda3/envs/pygmt/share/gmt version: 6.5.0

Minimal Complete Verifiable Example

No response

Full error message

No response

System information

PyGMT information:
  version: v0.11.0
System information:
  python: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:38:13) [GCC 12.3.0]
  executable: /home/william/anaconda3/envs/pygmt/bin/python
  machine: Linux-5.4.0-150-generic-x86_64-with-glibc2.27
Dependency information:
  numpy: 1.26.4
  pandas: 2.2.2
  xarray: 2024.3.0
  netCDF4: 1.6.5
  packaging: 24.0
  contextily: None
  geopandas: None
  ipython: None
  rioxarray: None
  ghostscript: 10.03.0
GMT library information:
  binary version: 6.5.0
  cores: 12
  grid layout: rows
  image layout: 
  library path: /home/william/anaconda3/envs/pygmt/lib/libgmt.so
  padding: 2
  plugin dir: /home/william/anaconda3/envs/pygmt/lib/gmt/plugins
  share dir: /home/william/anaconda3/envs/pygmt/share/gmt
  version: 6.5.0
welcome[bot] commented 6 months ago

👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. You might also want to take a look at our contributing guidelines and code of conduct.

seisman commented 6 months ago

Perhaps you need to set the fill like fill=data_dev['SGC_depth'].

yvonnefroehlich commented 6 months ago

I think there are two options to accomplish color-coding (and size-coding) (see the code example below):

Another aspect is that you set the size parameter, but before you have already set a fixed size via the style parameter.

Input data: MT-dev_inv-results.txt

import pandas as pd
import pygmt

file = "MT-dev_inv-results.txt"
cols2_use = [
    "Date", "Hour_UTC", "Latitude", "Longitude", "SGC_depth",
    "Lat-cent", "Lon-cent", "Mt_depth", "Variance",
    "VarRed", "Pdc", "Pclvd", "Piso", "Mw_x", "Mw_y",
]
data_dev = pd.read_csv(file, usecols=cols2_use, delimiter=" ")

region = pygmt.info(
    data=data_dev[["Longitude", "Latitude", "SGC_depth"]],
    per_column=True,
    spacing=(0.2, 0.2, 50),
)

fig = pygmt.Figure()

pygmt.makecpt(cmap="viridis", series=(100, 200, 1), continuous=None)

fig.plot3d(
    x=data_dev["Longitude"],
    y=data_dev["Latitude"],
    z=data_dev["SGC_depth"],
    fill=data_dev["SGC_depth"],
# OR
    # data=data_dev[["Longitude", "Latitude", "SGC_depth", "SGC_depth"]],
    cmap=True,
    style="u0.15c",
    region=region,
    # size=data_dev["Mw_x"]*1.2,  # Ignored when a fixed size via style is set
    perspective=[240, 30],
    zscale=-0.05,
)
fig.plot3d(
    # x=data_dev["Lon-cent"],
    # y=data_dev["Lat-cent"],
    # z=data_dev["Mt_depth"],
    # fill=data_dev["Mt_depth"],
# OR
    data=data_dev[["Lon-cent", "Lat-cent", "Mt_depth", "Mt_depth"]],
    cmap=True,
    style="u0.3c",
    region=region,
    perspective=[240, 30],
    zscale=-0.05,
    frame=[
        "WSneZ",
        "xa0.2f0.1g0.1+lLongitude [°]",
        "ya0.2f0.1g0.1+lLatitude [°]",
        "za50f20g20+lDepth [km]",
    ]
)

fig.colorbar(frame=["a20f10", "y+lkm"], position="JRM")

fig.show()

plot3d_colorcoding

WilliamEpenarandaA commented 6 months ago

Thanks for your comments. Including the line proposed by @seisman, solve the problem. I see that the input in fill parameter, need to put values of the variable used to make the correspond colorbar. I also thanks the explannation given to generate size-coding by @yvonnefroehlich