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

The color parameter of plot and plot3d methods can be 1d array #1075

Closed core-man closed 3 years ago

core-man commented 3 years ago

Description of the problem

We need to update the documentation for the color parameter in plot and plot3d because color can also be 1d array (originally posted in https://github.com/GenericMappingTools/pygmt/pull/1065#issuecomment-801212175).


See the description of the color parameter of plot:

color (str) – Select color or pattern for filling of symbols or polygons. Default is no fill.

See the description of the color parameter of plot3d:

color (str) – Select color or pattern for filling of symbols or polygons. Default is no fill.


However, color can also be a 1d array just like the sizes parameter when we use x/y parameters for in the plot and plot3d methods. See the last example in Plotting data points. We can set color=data.depth_km and data.depth_km is a 1d array.

import pygmt
data = pygmt.datasets.load_japan_quakes()
region = [
    data.longitude.min() - 1,
    data.longitude.max() + 1,
    data.latitude.min() - 1,
    data.latitude.max() + 1,
]
print(region)
print(data.head())

fig = pygmt.Figure()
fig.basemap(region=region, projection="M15c", frame=True)
fig.coast(land="black", water="skyblue")
pygmt.makecpt(cmap="viridis", series=[data.depth_km.min(), data.depth_km.max()])
fig.plot(
    x=data.longitude,
    y=data.latitude,
    sizes=0.02 * 2 ** data.magnitude,
    color=data.depth_km,
    cmap=True,
    style="cc",
    pen="black",
)
fig.colorbar(frame='af+l"Depth (km)"')
fig.show()
seisman commented 3 years ago

If I understand it correctly, we only need to update the docstrings to mention that color can be a string or a 1darray?

core-man commented 3 years ago

If I understand it correctly, we only need to update the docstrings to mention that color can be a string or a 1darray?

I think so. May better to add an additional note (Only valid if using x/y) like the size parameter for the plot method.

sizes (1d array) – The sizes of the data points in units specified using style. Only valid if using x/y.

Because we raise GMTInvalidInput when color is used with the data parameter just like the sizes parameter.

https://github.com/GenericMappingTools/pygmt/blob/1a2289a20519a4dbf1c78543acd4a654b981811e/pygmt/src/plot.py#L207-L211

https://github.com/GenericMappingTools/pygmt/blob/1a2289a20519a4dbf1c78543acd4a654b981811e/pygmt/src/plot.py#L214-L218

seisman commented 3 years ago

Sounds good. Maybe also need to mention that cmap is required if color is a 1darray (I don't check if it's true).

core-man commented 3 years ago

Sounds good. Maybe also need to mention that cmap is required if color is a 1darray (I don't check if it's true).

I think so. See GMT plot:

where the optional z is required when -C is used


@seisman I use the above Plot data points code but remove cmap=True. Cannot plot the symbols. So you are right.

camp
seisman commented 3 years ago

@core-man This issue looks easy to fix. Do you want to work on it?

core-man commented 3 years ago

@core-man This issue looks easy to fix. Do you want to work on it?

So we don't want to leave it to new contributors?

seisman commented 3 years ago

So we don't want to leave it to new contributors?

The description of the issue is so long that people may be lost about what they should do.

You may either submit a quick fix or give more details about the expected changes.