3

I have a GeoPandas GeoDataFrame which I am plotting against a specific column and the colour of the output/map is same for different assigned bins/intervals. The issue is because, sometimes, there is no value in/between the range assigned in the user defined scheme for the column/variable which we are plotting, like here

I have given 4 ranges

0 - 0.25,  
0.25 - 0.50,  
0.50 - 0.75,  
0.75 - 1.00 

but the variable/column from dataframe which I am plotting does not have any value in the range 0.75 - 1.00.

So it is assigning the same colour to the legend of the intervals 0.50 - 0.75, 0.75 - 1.00,

same colour legend

But I want to keep the bins values and legends static, as if, it should show the legends of 4 different colours only,

all legends

and it should ideally skip that interval/colour while plotting inside the polygons of the map if there is no value in that interval/range,

and it should still shows that interval/range in the legend.

Like, if here, there was no value between 0.75 to 1.00, then it may skip the green colour from the listed colour map assigned for that specific bound to plot inside the polygons,

but still shows that range/interval in the legend.

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

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

Also, if you will see the script, and look closely at the legends of 1st figure in my question, the color sequence which I have given in cmap = is in the order "red", "orange", "blue", "green",

but when the value in range (0.75 - 1.00) is missing, it is skipping the orange color and jumping to the blue (3rd color in the sequence) instead of picking the orange (2nd color in the sequence), i.e., not following a proper trend.

Is there any way possible?

for day in days_list:
    map_attribute = "far"
    df2 = csv_file[["state_id", "state_name", map_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.replace(np.nan,0)
    
    cmap = ListedColormap(["red", "orange", "blue", "green"])
    
    fig, ax = plt.subplots(figsize=(12,8))
    
    df_new.plot(
    ax=ax,
    column=map_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
    )
4

You just need to remove the last value (1.00) in bins list. Then the last legend item disappears. If it doesn't, it means that there is at least one item in range (0.75 - 1.00).

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

For example, column VALUE range is 1 - 764 in my data. When I use classification_kwds={'bins':[20, 50, 764, 1000]}, the result is:

enter image description here

When I remove the last value, the result is:

enter image description here

6
  • Hiii @kadir, thanks for the time, but what I meant by keeping the bin values and legends static was that, I want to keep the bin values same coz I have a large dataset and I want to iterate the same bin values over that dataset for generating map. Some of those datasets might not be having values in the range of specific bin interval, so I just want that matplotlib somehow skip that specific colour to fill in the polygon. – Abhilash Singh Chauhan Dec 23 '20 at 5:47
  • Legends are not an issue, they can be added separately, but if you look closely at the figures in my question, the colour sequence which I have given in cmap = is in the order "red", "orange", "blue", "green", but when the value in range (0.75 - 1.00) is missing, it is skipping the orange colour and jumping to the blue (3rd colour in the sequence) instead of picking the orange (2nd colour in the issue) in 1st figure, else colour fills won’t be a problem if it already picked the proper colour in sequence for the lower and upper bounds. – Abhilash Singh Chauhan Dec 23 '20 at 5:48
  • @AbhilashSinghChauhan I noticed that. But I couldn't understand the reason of skipping the colors. – Kadir Şahbaz Dec 23 '20 at 11:50
  • skipping like, in the 1st figure kindly see that, when when there is no value in the last range which is 4th interval here (0.75 - 1.00): for which green color is assigned, it should not pick the green color to fill in polygons, rather it should have taken the first 3 colors for the respective intervals, i.e red", "orange" and "blue" @kadir – Abhilash Singh Chauhan Dec 23 '20 at 12:01
  • When I run the script it skipped one of the middle colors (for example, in this case orange). I didn't figure it out why. Therefore I added the result with three colors in cmap – Kadir Şahbaz Dec 23 '20 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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