【发布时间】:2021-07-22 12:28:59
【问题描述】:
为什么我的图只显示 1 个图例,希望图例显示两个 DataFrame 的标签。我发现了一个类似的问题,但我无法将其应用于我的数据:Legend only shows one label when plotting with pandas 资料:https://github.com/DwightZC/Learning
data=pd.read_csv('Data gathered1.csv')
data
data['CONTAMINANTS'] = data['CONTAMINANTS'].str.split(pat=', ')
data_long = data.explode('CONTAMINANTS')
data_long['CONTAMINANTS'].value_counts()
ACT = {'0': 'No Activity',
'1A' : 'CONTAMINATION CONFIRMED',
'1B' : 'CONTAMINATION CONFIRMED',
'2A' :'INVESTIGATION',
'2B': 'INVESTIGATION',
'3':'CORRECTIVE ACTION PLANNING',
'4': 'IMPLEMENT ACTION',
'5': 'MONITOR ACTION',
'6':'INACTIVE'
}
data['STATUS'] = data['ACT-STATUS'].apply(lambda x: ACT[x])
data
color = { 'No Activity': 'black',
'CONTAMINATION CONFIRMED':'lightblue',
'INVESTIGATION':'red',
'CORRECTIVE ACTION PLANNING':'pink',
'IMPLEMENT ACTION':'yellow',
'MONITOR ACTION':'green',
'INACTIVE':'gray'
}
data['COLOR'] = data['STATUS'].apply(lambda x: color[x])
data
x=data['LONGITUDE']
y= data["LATITUDE"]
import cartopy.io.shapereader as shpreader
reader = shpreader.Reader('cb_2018_us_county_5m')
counties = list(reader.geometries())
COUNTIES = cfeature.ShapelyFeature(counties, ccrs.PlateCarree())
reader2 = shpreader.Reader('City')
city = list(reader2.geometries())
Cities = cfeature.ShapelyFeature(city, ccrs.PlateCarree())
import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
import io
from urllib.request import urlopen, Request
from PIL import Image
def image_spoof(self, tile): # this function pretends not to be a Python script
url = self._image_url(tile) # get the url of the street map API
req = Request(url) # start request
req.add_header('User-agent','Anaconda 3') # add user agent to request
fh = urlopen(req)
im_data = io.BytesIO(fh.read()) # get image
fh.close() # close url
img = Image.open(im_data) # open image with PIL
img = img.convert(self.desired_tile_form) # set image format
return img, self.tileextent(tile), 'lower' # reformat for cartopy
cimgt.OSM.get_image = image_spoof # reformat web request for street map spoofing
osm_img = cimgt.OSM() # spoofed, downloaded street map
fig = plt.figure(figsize=(12,9)) # open matplotlib figure
ax1 = plt.axes(projection=osm_img.crs) # project using coordinate reference
system (CRS) of street
mapcenter_pt = [26.2271, -98.2087] # lat/lon hidalgo
zoom = 0.5 # for zooming out of center point
extent = [center_pt[1]-(zoom*2.0),center_pt[1]+(zoom*2.0),center_pt[0]-
zoom,center_pt[0]+zoom] #
adjust to zoom
ax1.set_extent(extent) # set extents
ax1.scatter(x,y,c=data['COLOR'], transform=ccrs.PlateCarree())
scale = np.ceil(-np.sqrt(2)*np.log(np.divide(zoom,350.0))) # empirical solve
for scale based on zoom
scale = (scale<20) and scale or 19 # scale cannot be larger than 19
ax1.add_image(osm_img, int(scale)) # add OSM with zoom specification
ax1.set_title("Hidalgo County")
#ax1.add_feature(COUNTIES, facecolor='none', edgecolor='gray')
#ax1.add_feature(Cities, facecolor='none', edgecolor='gray')
plt.show()
【问题讨论】:
-
检查
ACT-STATUS列是否仅包含ACT字典的键,并检查当列项为4,5A时会发生什么(来自MSW,C & T LANDFILL记录) -
嗨,我想通了。谢谢你。我重新编辑我的帖子
标签: dataframe matplotlib openstreetmap scatter-plot cartopy