4

I have a GeoPandas GeoDataFrame which I am plotting against a specific column and the colour of the output/map is not matching with the colour assigned to different bins/intervals. It should ideally be unique for distinct intervals but the legends are taking same colour for different intervals

##colour

    cmap = ListedColormap(["red", "orange", "blue", "green"])

## bins/intervals

    classification_kwds={'bins':[0.25, 0.50, 0.75, 1.00]}

Same colour legend for 2 different intervals is coming in the output. Why it is happening so?

Same colour legend

The code is as follows


for day in days_list:
    attribute = "far" 
    df2 = csv_file[["state_id", "state_name", attribute]].loc[csv_file["days"] == day] 
    new_df = df1.merge(df2, on = 'state_id', how="left")
    new_df.drop(columns=['state_name_y'], inplace = True)
    new_df.rename(columns={"state_name_x": "state_name"}, inplace = True)
    df_new = new_df.fillna(0)
    
    cmap = ListedColormap(["red", "orange", "blue", "green"])

    fig, ax = plt.subplots(figsize=(12,8))
 
    df_new.plot(
        ax=ax,
        column=attribute,
        linewidth=1,
        cmap=cmap,
        scheme='User_Defined',
        edgecolor='black',
        alpha=0.9,
        classification_kwds={'bins':[0.25, 0.50, 0.75, 1.00]},
        legend=True
    )
    
    ax.set_title("Day "+ str(day)+ " " + str(attribute))
    
    plt.tight_layout()

    plt.savefig("day_{}.png".format(day), dpi=300)

This is the Geopandas GeoDataFrame

1 2

1 Answer 1

3

Try to change column parameter from df_new[attribute] to attribute in plot method.

df_new.plot(
    ax=ax,
    column=attribute, ## change here
    ...
    ...
    )

If it doesn't work, you may be using the wrong column name or wrong bins values. Check the range of "far" column. In your previous question you use another column name and the same bins values for the same script, but styling looks like correct.

4
  • I have tried as suggested, but it didn't worked, The column name is correct though. I have attached the table, I Can share the dataset, if you wanna to have a look. Thanks for the suggestions though. Dec 21, 2020 at 20:14
  • The range of “far” is between 0 and 1 (please see the range from the table), and the bins value are within this range only. In this script, I have just assigned the new variable “attribute”, so that i don’t have to change the column name everywhere in the script. But it’s now working. Dec 21, 2020 at 20:36
  • 2
    If you found where the problem was and what the solution was, please add your solution as an answer so that future users who have the same problem may benefits from the solution. Dec 21, 2020 at 20:42
  • Hi @Kadir, I saw the dataframe precisely again. Actually, there were no values between the 4th interval (0.75 - 1.00), that's why it was assigning same colour to both the intervals (0.50 - 0.75 and 0.75 - 1.00). But I want to keep the user assigned bins static i.e don't want to change the intervals assigned. Is there any way that to proceed, like if the value in specific range is not available, then matplotlib skip that interval while plotting inside the polygons, and still shows that range in the legend? Dec 22, 2020 at 9:01

Your Answer

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

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