Skip to content
Advertisement

Matplotlib & Cartopy – How to automatically plot data with colors depending on a categorical column?

I am trying to plot ship trajectories in Cartopy, but am unable to automatically use a column of my df to set the color groups of the data.

JavaScript

This throws the error ‘dtype: object is not a valid value for color’. In Geopandas you can set the color groups using the ‘column’ field. Is there anything similar for Matplotlib/Cartopy?

Thanks in advance for any help provided!

Advertisement

Answer

You can use ax.scatter with the c keyword argument pointing to a DataFrame column of integer. If you need to convert text to a integer label, it can be done easily with the sklearn.preprocessing.LabelEncoder.

Here is an example using your map instance:

JavaScript

The points with the same ‘Continent’ label have the same integer label code and color.

enter image description here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement