Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    3 Steps to Plot Shooting Incident in NY on a Map Using Python

    What will you learn in this tutorial?

    • Where to find interesting data contained in CSV files.
    • How to extract a map to plot the data on.
    • Use Python to easily plot the data from the CSV file no the map.

    Step 1: Collect the data in CSV format

    You can find various interesting data in CSV format on data.world that you can play around with in Python.

    In this tutorial we will focus on Shooting Incidents in NYPD from the last year. You can find the data on data.world.

    data.world with NYPD Shooting Incident Data (Year To Date)
    data.world with NYPD Shooting Incident Data (Year To Date)

    You can download the CSV file containing all the data by pressing on the download link.

    To download CSV file press the download.
    To download CSV file press the download.

    Looking at the data you see that each incident has latitude and longitude coordinates.

    {'INCIDENT_KEY': '184659172', 'OCCUR_DATE': '06/30/2018 12:00:00 AM', 'OCCUR_TIME': '23:41:00', 'BORO': 'BROOKLYN', 'PRECINCT': '75', 'JURISDICTION_CODE': '0', 'LOCATION_DESC': 'PVT HOUSE                     ', 'STATISTICAL_MURDER_FLAG': 'false', 'PERP_AGE_GROUP': '', 'PERP_SEX': '', 'PERP_RACE': '', 'VIC_AGE_GROUP': '25-44', 'VIC_SEX': 'M', 'VIC_RACE': 'BLACK', 'X_COORD_CD': '1020263', 'Y_COORD_CD': '184219', 'Latitude': '40.672250312', 'Longitude': '-73.870176252'}

    That means we can plot on a map. Let us try to do that.

    Step 2: Export a map to plot the data

    We want to plot all the shooting incidents on a map. You can use OpenStreetMap to get an image of a map.

    We want a map of New York, which you can find by locating it on OpenStreetMap or pressing the link.

    OpenStreetMap (sorry for the Danish language)

    You should press the blue Download in the low right corner of the picture.

    Also, remember to get the coordinates of the image in the left side bar, we will need them for the plot.

    map_box = [-74.4461, -73.5123, 40.4166, 41.0359]

    Step 3: Writing the Python code that adds data to the map

    Importing data from a CVS file is easy and can be done through the standard library csv. Making plot on a graph can be done in matplotlib. If you do not have it installed already, you can do that by typing the following in a command line (or see here).

    pip install matplotlib

    First you need to transform the CVS data of the longitude and latitude to floats.

    import csv
    # The name of the input file might need to be adjusted, or the location needs to be added if it is not located in the same folder as this file.
    csv_file = open('nypd-shooting-incident-data-year-to-date-1.csv')
    csv_reader = csv.DictReader(csv_file)
    longitude = []
    latitude = []
    for row in csv_reader:

    Now you have two lists (longitude and latitude), which contains the coordinates to plot.

    Then for the actual plotting into the image.

    import matplotlib.pyplot as plt
    # The boundaries of the image map
    map_box = [-74.4461, -73.5123, 40.4166, 41.0359]
    # The name of the image of the New York map might be different.
    map_img = plt.imread('map.png')
    fig, ax = plt.subplots()
    ax.scatter(longitude, latitude)
    ax.set_ylim(map_box[2], map_box[3])
    ax.set_xlim(map_box[0], map_box[1])
    ax.imshow(map_img, extent=map_box, alpha=0.9)

    This will result in the following beautiful map of New York, which highlights where the shooting in the last year has occurred.

    Shootings in New York in the last year. Plot by Python using matplotlib.
    Shootings in New York in the last year. Plot by Python using matplotlib.

    Now that is awesome. If you want to learn more, this and more is covered in my online course. Check it out.

    You can also read about how to plot the mood of tweets on a leaflet map.

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment