7

I have a CSV of all earthquakes in my country for the last 10 years arranged by date and time. My goal is to plot each line chronologically one by one, so that I can compile these maps and produce a "time-lapse" of the earthquakes. The CSV is already sorted but I'll code the "sorting" function when I get there.

So if I have 10 earthquakes, the end products are 10 maps with each a single plot of where the earthquake happened.

My code:

from shapely.geometry import Point
import geopandas as gpd
from geopandas import GeoDataFrame
import pandas as pd
import os

os.chdir(r'path')

def plotPoint():
    df = pd.read_csv('earthquakes.csv')
    geometry = [Point(xy) for xy in zip(df['Longitude'], df['Latitude'])]
    crs = "epsg:32651"
    gdf = GeoDataFrame(df, crs=crs, geometry=geometry)
    basemap = gpd.read_file('basemap.shp')
    for i in range(len(gdf)): # Counts how many rows there are then loops through each line of "gdf"
        if i == 0: # Skips over first line since it is the column header
            continue
        pt = gdf.plot(ax=basemap.plot(figsize=(10,6)), marker='o', color='red', markersize=15)
        print(pt[i]) # Executes "pt" one line at a time
plotPoint()

When I run this, I get

File "C:\path\file.py", line 26, in plotPoint
    print(pt[i]) 
TypeError: 'AxesSubplot' object is not subscriptable

I'm still new to Python. Any leads?

2
  • Please Edit the question to indicate where the error occurs.
    – Vince
    May 21, 2020 at 14:26
  • Done. Thank you for editing! @Vince
    – BallpenMan
    May 21, 2020 at 14:32

1 Answer 1

10

The following script saves an image for each earthquake point in a single plot.

from shapely.geometry import Point
from geopandas import GeoDataFrame
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
import os

os.chdir(r'path')

def plotPoint():
    df = pd.read_csv('earthquakes.csv')
    basemap = gpd.read_file('basemap.shp')
    crs = "epsg:32651"
    geometry = [Point(xy) for xy in zip(df['Longitude'], df['Latitude'])]
    gdf = GeoDataFrame(df, crs=crs, geometry=geometry)

    for i in range(gdf.shape[0]):
        ax = basemap.plot(figsize=(10,6))
        g = gdf.iloc[i].geometry
        plt.plot(g.x, g.y, marker='o', color='red', markersize=15)
        plt.savefig("earthquake_{0}.png".format(i))

plotPoint()

Result: (Three plots in the image are separate and each plot is saved to current working directory as separate images)

enter image description here

2
  • Wow! This worked! You wrote the entire code. I'll understand each line in the for loop, some of those things are new to me. Thank you!
    – BallpenMan
    May 21, 2020 at 14:54
  • That full code will be useful for me as well. Thank you! May 22, 2020 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.