[Python] How can I plot a column that's full of nan values?

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I have code that runs through a geojson file and plots columns from that file one by one to show the user the costs of an optimisation. It ran fine, until it came across a column of just nan values.

    I did some looking into this and discovered that the Geopandas (v0.14.4) plotting file firstly creates a list of which values are nan:

    nan_idx = np.asarray(pd.isna(values), dtype="bool")


    then it resets the values variable to contain all the values that are not nan:

    values = cat.codes[~nan_idx]


    and then it fills nans with a placeholder so that it can properly plot them:

    for n in np.where(nan_idx)[0]:
    values = np.insert(values, n, values[0])


    and this is where the issue is! As the column just contains nans, I get an IndexError from values[0] as it's now empty. This was reconfirmed by changing one value to a float and it working perfectly; plotting the nans as missing values.

    Can anyone tell me if there is another way to plot columns containing all nan values or if this issue has been developed upon in newer versions of GeoPandas? It's important to show that the values are all "missing values", as that will just show that that specific column is not feasible.

    A snippet of the code is (where name is the column name and hexagons refer to a Geopandas df):

    fig = plt.figure(figsize=figsize)
    ax = plt.axes(projection=crs)
    ax.set_axis_off()

    hexagons.to_crs(crs.proj4_init).plot(
    ax=ax,
    column = name,
    legend = legend,
    cmap = cmap,
    legend_kwds = legend_kwds,
    missing_kwds = missing_kwds,
    )

    ax.set_title(name)
    fig.savefig(output_folder + f"/{name}.png", bbox_inches=bbox_inches)
    plt.close()

    Continue reading...

Compartilhe esta Página