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

Time:04-17

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?

CodePudding user response:

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

CodePudding user response:

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 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

  • Related