How to plot a scatter plot on top of basemap with varying densities

I am trying to plot the pollution levels at different years and of different locations in India.

I am able to get the scatter plot and the basemap independently. However, I am facing difficulty in plotting this scatter plot on the specified geographic location on the map. The result is that the map is being placed ON TOP of the scatter plot, which is not what I want.

Here is the code I am using:

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import pandas as pd
m = Basemap(projection='mill',
            llcrnrlat = -80,#llcrncrlat=lower left corner latitude
            llcrnrlon = -180,
            urcrnrlat = 80,
            urcrnrlon = 180,
            resolution = 'l')
df = pd.read_csv('dust.csv')
x = df[['y']]
y = df[['x']]
colors = df[['y1']]
#m.drawcoastlines()
#m.drawcountries(linewidth=2)
#m.scatter(df['x'],df['y'],s=colors, alpha=0.5, cmap='viridis')
plt.scatter(x,y,s=colors, alpha=0.5, cmap='viridis')
plt.colorbar()
plt.show()

Upvotes: 2

Views: 1336

Answers (2)

Problem: How to plot the scatter plot with varying densities on TOP of a basemap?

Data: I am having the latitudes and longitudes of various cities in India along with their pollution levels of various years.

Approach: Initially, I have assigned a assigned the datastructure to a variable 'df'. Assigned the latitudes to 'y' and longitudes to 'x'. Since the latitudes and longitudes also posses a variable 'y' and 'x', within it, I have used 'df['x'].values' to get the numeric values alone. Taking into the consideration the geometric position of India, I have plotted the map. Next set is to take in these latitudes and longitudes into the same variable as the map. This was crucial without which, I could not observe the scatter plot. Taking the color as the third variable I have plotted the scatter plot and the pollution levels have varying densities.

Map of India containing the pollution levels at different cities of the year 2013

CODE:

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

# make up some data for scatter plot
df = pd.read_csv('dust.csv')
x = df[['y']].values
y = df[['x']].values
fig = plt.gcf()
fig.set_size_inches(8, 6.5)

m = Basemap(projection='mill',
            llcrnrlat = 6.5,#llcrncrlat=lower left corner latitude
            llcrnrlon = 66,
            urcrnrlat = 36,
            urcrnrlon = 98,
            resolution = 'l')

#m.bluemarble(scale=0.2)   # full scale will be overkill
m.drawcoastlines(linewidth=1)# add coastlines
m.drawcountries(linewidth=1)

# transform coordinates
#plt.subplot(221)
x,y=m(x,y)
plt.scatter(x, y,s=df.iloc[1:,2], alpha = 0.5, cmap='viridis')
plt.colorbar()


plt.show()

Upvotes: 1

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

# make up some data for scatter plot
df = pd.read_csv('dust.csv')
x = df[['y']].values
y = df[['x']].values
fig = plt.gcf()
fig.set_size_inches(8, 6.5)

m = Basemap(projection='mill',
            llcrnrlat = 6.5,#llcrncrlat=lower left corner latitude
            llcrnrlon = 66,
            urcrnrlat = 36,
            urcrnrlon = 98,
            resolution = 'l')

#m.bluemarble(scale=0.2)   # full scale will be overkill
m.drawcoastlines(linewidth=1)# add coastlines
m.drawcountries(linewidth=1)

# transform coordinates
#plt.subplot(221)
x,y=m(x,y)
plt.scatter(x, y,s=df.iloc[1:,2], alpha = 0.5, cmap='viridis')
plt.colorbar()


plt.show()

Upvotes: 1

Related Questions