GenericMappingTools / pygmt

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

Some vectors lost head #3282

Closed sungho91 closed 5 months ago

sungho91 commented 5 months ago

Description of the problem

Hi

All vectors should have a head but some of them don't have (orange-dotted box in att.). How can I fix them?

Best,

Sungho

vel_1deg_NNR

Minimal Complete Verifiable Example

# Define a scaling factor to scale the vectors appropriately
scaling_factor = 0.1  # Vector length
arrow_width = 0.01  # Arrowhead width
arrow_length = 0.1  # Arrowhead length
freq  = 64
fig = pygmt.Figure()
fig.coast(region="g", projection="N12", land="gray", frame="af")
fig.plot(
    region="g",
    projection="N12",
    x=longitude_gps.flatten()[::freq],
    y=latitude_gps.flatten()[::freq],
    direction=[gps_velocity_direction.flatten()[::freq], 0.01*gps_velocity_length.flatten()[::freq]],
    style=f'v{scaling_factor}c+e{arrow_width}c+h{arrow_length}c',
    # style=f'v0.2+e+h0.3',
    pen="0.3p,red3",
    color="red3",
)
fig.savefig('vel_1deg_NNR.png')
fig.show()

Full error message

No response

System information

I tested it on M1 macbook and Ubuntu 22.04
welcome[bot] commented 5 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.

yvonnefroehlich commented 5 months ago

Hello @sungho91,

As we do not have your input data, we cannot run your script.

However, I think the vectors within the orange-dashed box are drawn without heads because the vector length is too short relatively to the size of the heads. Please find below a general code example showing this:

import numpy as np
import pygmt

x = np.linspace(0.2, 0.2, 6)  # x vector coordinates
y = np.linspace(-2, 2, 6)  # y vector coordinates
direction = np.zeros(x.shape)  # direction of vectors
length = np.linspace(0.1, 1.4, 6)  # length of vectors

fig = pygmt.Figure()

fig.basemap(region=[0, 10, -2.5, 2.5], projection="X5c/3c", frame=0)
# Plot Cartesian vectors with different lengths and diffent head sizes
fig.plot(x=x, y=y, style="v0.4c+e", direction=[direction, length])
fig.plot(x=x+3.3, y=y, style="v0.2c+e", direction=[direction, length])
fig.plot(x=x+6.6, y=y, style="v0.1c+e", direction=[direction, length])

fig.show()

arrows_heads_disapear

sungho91 commented 5 months ago

Hi @yvonnefroehlich

Thank you for your comment. I've attached my files to this. So, if the length of velocity is too small, there is no way to plot them with a head?

Archive.zip

seisman commented 5 months ago

I think you need to use +n in the style parameter. See https://docs.generic-mapping-tools.org/dev/plot.html#vector-attributes

yvonnefroehlich commented 5 months ago

I think you need to use +n in the style parameter. See https://docs.generic-mapping-tools.org/dev/plot.html#vector-attributes

I just modified my code example to compare the results without (black) and with (red) using +n:

# Modified from https://www.pygmt.org/dev/gallery/lines/vector_styles.html#sphx-glr-gallery-lines-vector-styles-py
# Last access 2024/06/03

import numpy as np
import pygmt

x = np.linspace(0.4, 0.4, 4)  # x vector coordinates
y = np.linspace(-1, 1, 4)  # y vector coordinates
direction = np.zeros(x.shape)  # direction of vectors
length = np.linspace(0.1, 0.5, 4)  # length of vectors

fig = pygmt.Figure()
fig.basemap(region=[0, 3, -1.5, 1.5], projection="X3c/2c", frame=0)

for keep_head in ["", "+n"]:

    match keep_head:
        case "": pen = "0.7p,black"
        case "+n": pen = "0.2p,red"

    # Plot Cartesian vectors with different lengths and diffent head sizes
    fig.plot(x=x, y=y, style=f"v0.4c+e{keep_head}", pen=pen, direction=[direction, length])
    fig.plot(x=x+1, y=y, style=f"v0.2c+e{keep_head}", pen=pen, direction=[direction, length])
    fig.plot(x=x+2, y=y, style=f"v0.1c+e{keep_head}", pen=pen, direction=[direction, length])

    fig.show(dpi=720)

arrows_heads_disapear_back_with_n

sungho91 commented 5 months ago

This is really helpful. It just occurred to me. I just adjusted very small size lengths to a certain magnitude. FYI, it also works.

vel_1deg_NNR