[Python] Change .shp polygon color based on .csv single cell value

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have a pyplot with three .shp polygons as sublots. I would like to change the color of each polygon based on the values from a .csv file of 3 columns and 2 rows (headers and values). I would like to run each polygon through 2 thresholds. For example, >5= yellow, >10= Red, else blue. I intend to automate the .csv, so the polygons change colors based on the values in the .csv file. I have tried conditional arguments but not get anything to work.

    import geopandas as gpd
    import matplotlib.pyplot as plt
    import pandas as pd
    import csv


    gdf1 = gpd.read_file("FDRA_1.shp")
    gdf2 = gpd.read_file("FDRA_2.shp")
    gdf3 = gpd.read_file("FDRA_3.shp")`

    row = 2; col = 1
    with open("Valley_Index.csv") as f:
    reader = csv.reader(f)
    for i in range(row):
    row = next(reader)
    VIndex = row[col - 1]
    threshold1=5


    fig, ax = plt.subplots()

    for i in VIndex:
    color = 'coral' if VIndex > threshold1 else 'blue'
    ax.fill(x, y, color=color)

    gdf1.plot(ax=ax, color='blue', edgecolor='black')


    gdf2.plot(ax=ax, color='red', edgecolor='black')


    gdf3.plot(ax=ax, color='green', edgecolor='black')


    plt.title('Multiple Polygons')
    plt.xlabel('Longitude')
    plt.ylabel('Latitude')


    plt.show()

    Continue reading...

Compartilhe esta Página