'How do I use maltplotlib to assign different colors according to the country from a Csv?

I have a data frame with the values "city, country, longitude, latitude" and I want to create a scatter plot of different cities' longitude and latitude, but also color them according to the country they are in. So, all the cities in the same country should have the same color.

This is the code I have right now:

plt.figure(figsize=(15, 12))
plt.title('Longitude and latitude based on country',fontsize=15)
plt.scatter(df['longitude'], df['latitude'], s=100)
plt.show()

It plots the information but I am not sure how to go about coloring the dots according to the country. How can this be accomplished?



Solution 1:[1]

Add a colour column to df and then:

plt.figure(figsize=(15, 12))
plt.title('Longitude and latitude based on country',fontsize=15)
plt.scatter(df['longitude'], df['latitude'], c=df['colour'], s=100)
plt.show()

eg:

df = pd.DataFrame()
df['longitude'] = [1,2,3,3,4,44,5]
df['latitude'] = [123061,12381,2037,1232,23,232,1]
df['colour'] = ['red']*7
plt.figure(figsize=(15, 12))
plt.title('Longitude and latitude based on country',fontsize=15)
plt.scatter(df['longitude'], df['latitude'], c=df['colour'], s=100)
plt.show()

enter image description here

Solution 2:[2]

Are you trying to achieve something like this?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

//Below the values will be according to your csv I don't have any knowledge about longitude and latitude I am using random for now

longitude = np.random.rand(100)
latitude = np.random.randint(100,600,100)
country =['USA','INDIA', 'JAPAN', 'Australia']*25

df = pd.DataFrame(dict(longitude=longitude, latitude=latitude, country = country))

fig, ax = plt.subplots()

colors = {'USA':'blue', 'INDIA':'green', 'JAPAN':'red', 'Australia':'yellow'}


ax.scatter(df['longitude'], df['latitude'], c=df['country'].map(colors))

plt.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nin17
Solution 2