简体   繁体   English

如何 plot 将 plot 散布在 matplotlib 的底图顶部,密度不同

[英]How to plot scatter plot on TOP of matplotlib's basemap with varying densities

I am trying to plot the pollution levels at different years and of different locations in India.我正在尝试 plot 印度不同年份和不同地点的污染水平。

I am able to get the scatter plot and the basemap independently.我能够独立获得散点图 plot 和底图。 However, I am facing difficulty in plotting this scatter plot on the specified geographic location on the map.但是,我很难在 map 的指定地理位置上绘制这个散点 plot。 The result is that the map is being placed ON TOP of the scatter plot, which is not what I want.结果是 map 被放置在散布 plot 的顶部,这不是我想要的。

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

Problem: How to plot the scatter plot with varying densities on TOP of a basemap?问题:如何 plot 散布 plot 在底图的 TOP 上具有不同的密度?

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'.方法:最初,我已将分配的数据结构分配给变量“df”。 Assigned the latitudes to 'y' and longitudes to 'x'.将纬度分配给“y”,将经度分配给“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.由于纬度和经度也有一个变量'y'和'x',在其中,我使用'df['x'].values'来单独获取数值。 Taking into the consideration the geometric position of India, I have plotted the map.考虑到印度的几何 position,我绘制了 map。 Next set is to take in these latitudes and longitudes into the same variable as the map.下一组是将这些纬度和经度纳入与 map 相同的变量中。 This was crucial without which, I could not observe the scatter plot.这是至关重要的,没有它,我无法观察到散射 plot。 Taking the color as the third variable I have plotted the scatter plot and the pollution levels have varying densities.以颜色作为第三个变量,我绘制了散点图 plot 并且污染水平具有不同的密度。

印度地图包含 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM