Search code examples
pythonpygmt

Python PyGMT Plotting Data Points from CSV


I try the tutorial here:

https://www.pygmt.org/latest/tutorials/basics/plot.html#sphx-glr-tutorials-basics-plot-py

But, instead of getting their already have example of japan_quakes I create my own csv that can works for the first two examples. But won't work at the last example (the figure won't show up)

this is the problematic one:

import pygmt
import pandas as pd

data = pygmt.datasets.load_sample_data(name="japan_quakes")

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,
    size=0.02 * 2**data.magnitude,
    fill=data.depth_km,
    cmap=True,
    style="cc",
    pen="black",
)
fig.colorbar(frame="af+lDepth (km)")
fig.show()

I also want to ask:

How to create slider that can change the plots based on year? thus if the slide is on 1987 it will shows all the data of 1987


Solution

  • To save a figure generated with PyGMT you can use the pygmt.Figure.savefig method, for details please have a look in the documentation at https://www.pygmt.org/dev/api/generated/pygmt.Figure.savefig.html.

    I expanded the code of the gallery example to save the final figure:

    import pygmt
    import pandas as pd
    
    data = pygmt.datasets.load_sample_data(name="japan_quakes")
    
    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,
        size=0.02 * 2**data.magnitude,
        fill=data.depth_km,
        cmap=True,
        style="cc",
        pen="black",
    )
    fig.colorbar(frame="af+lDepth (km)")
    # fig.show()
    fig.savefig(
        fname="japan_quakes.png",  # save the figure as PNG file
        dpi=360,  # with a resolution of 360 dpi
    )