Dataframe 我的颜色图的标签只显示了一个

Dataframe 我的颜色图的标签只显示了一个,dataframe,matplotlib,openstreetmap,scatter-plot,cartopy,Dataframe,Matplotlib,Openstreetmap,Scatter Plot,Cartopy,为什么我的图只显示1个图例,我希望图例显示两个数据帧的标签。我发现了类似的问题,但我无法将其应用于我的数据: 数据: data=pd.read\u csv('data gathered1.csv')) 数据 数据['polictures']=数据['polictures'].str.split(pat=',') data_long=data.explode(‘污染物’) 数据长度['污染物]。值计数() ACT={0':“无活动”, ‘1A’:‘已确认污染’, ‘1B’:‘已确认污染’, ‘2A

为什么我的图只显示1个图例,我希望图例显示两个数据帧的标签。我发现了类似的问题,但我无法将其应用于我的数据: 数据:

data=pd.read\u csv('data gathered1.csv'))
数据
数据['polictures']=数据['polictures'].str.split(pat=',')
data_long=data.explode(‘污染物’)
数据长度['污染物]。值计数()
ACT={0':“无活动”,
‘1A’:‘已确认污染’,
‘1B’:‘已确认污染’,
‘2A’:‘调查’,
‘2B’:‘调查’,
“3”:“纠正措施计划”,
“4”:“实施行动”,
“5”:“监视操作”,
“6”:“不活动”
}
数据['STATUS']=数据['ACT-STATUS']。应用(lambda x:ACT[x])
数据
颜色={‘无活动’:‘黑色’,
“已确认污染”:“浅蓝色”,
"调查":"红色",,
“纠正措施计划”:“粉色”,
“实施操作”:“黄色”,
“监视操作”:“绿色”,
“非活动”:“灰色”
}
数据['COLOR']=数据['STATUS']。应用(lambda x:COLOR[x])
数据
x=数据[‘经度’]
y=数据[“纬度”]
将cartopy.io.shapereader作为shpreader导入
reader=shpreder.reader('cb\U 2018\U us\U county\U 5m')
县=列表(reader.geometrics())
Countries=cfeature.ShapelyFeature(Countries,ccrs.PlateCarree())
reader2=shpreader.Reader('City')
城市=列表(reader2.geometrics())
Cities=cfeature.ShapelyFeature(城市,ccrs.PlateCarree())
将matplotlib.pyplot作为plt导入
将numpy作为np导入
将cartopy.crs作为CCR导入
将cartopy.io.img_瓷砖作为cimgt导入
输入io
从urllib.request导入urlopen,请求
从PIL导入图像
def image_spoof(self,tile):#此函数假装不是Python脚本
url=self._image_url(tile)#获取街道地图API的url
请求=请求(url)#启动请求
请求添加标题(“用户代理”,“Anaconda 3”)#向请求添加用户代理
fh=urlopen(请求)
im_data=io.BytesIO(fh.read())#获取图像
fh.close()#关闭url
img=Image.open(im#u数据)#使用PIL打开图像
img=img.convert(自身所需的_tile_form)#设置图像格式
返回img,self.tileextent(tile),“lower”#重新格式化为cartopy
cimgt.OSM.get_image=image_spoof#重新格式化街道地图欺骗的web请求
osm_img=cimgt.osm()#伪造、下载的街道地图
图=plt.图(figsize=(12,9))#打开matplotlib图
ax1=plt.轴(投影=osm_img.crs)#使用坐标参考投影
街道交通系统(CRS)
地图中心点=[26.2271,-98.2087]#拉特/隆伊达尔戈
缩放=0.5#用于缩小中心点
范围=[中心点[1]-(缩放*2.0),中心点[1]+(缩放*2.0),中心点[0]-
缩放,中心点[0]+缩放]#
调整以缩放
ax1.设置范围(范围)#设置范围
ax1.scatter(x,y,c=data['COLOR'],transform=ccrs.PlateCarree())
scale=np.ceil(-np.sqrt(2)*np.log(np.divide(zoom,350.0))#经验解
用于基于缩放的缩放
量表=(量表

我把你的数据文件下载到我的电脑上,然后写了以下内容

import numpy as np
import matplotlib.pyplot as plt
from csv import reader

# the following two dicts are copied from your question
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'}
color = {'No Activity': 'black', 'CONTAMINATION CONFIRMED':'lightblue',
         'INVESTIGATION':'red', 'CORRECTIVE ACTION PLANNING':'pink',
         'IMPLEMENT ACTION':'yellow', 'MONITOR ACTION':'green', 'INACTIVE':'gray'}
# but we don't need ACT, we need its inverse…
ACT2codes = {}
for k, v in ACT.items(): ACT2codes.setdefault(v, []). append(k)

# ######################## let's read the data ########################
# lines is a generator, returns lines split on commas (respecting quoting)
# data is a dictionary of tuples of strings, indexed by the headers
lines = reader(open('hidalgo.csv', 'r'))    
data = {k:v for k, v in zip(next(lines), zip(*lines))}    
# but something it's better understood as an array of floats
for k in ('LONGITUDE',  'LATITUDE'):
    data[k] = np.array([float(item) for item in data[k]])        
# and something else is better understood as an array of strings,
# because we'll use np.where to find the indices required for plotting
data['ACT-STATUS'] = np.array(data['ACT-STATUS'])    
# ######################## ready to plot ########################
plt.figure(constrained_layout=1)
# for each action, plot some points with same color and same label
for action in ACT2codes.keys():
    # what are the indices of this batch of points?
    idx = []
    for code in ACT2codes[action]:
        idx += list(*np.where(data['ACT-STATUS']==code))
    plt.scatter(data['LONGITUDE'][idx],
                data['LATITUDE'][idx],
                color=color[action], label=action)
plt.legend() ; plt.show()

很抱歉,我不太了解熊猫,所以我没有用过它…另一方面,似乎使用csv模块来处理数据并不太复杂-此外,我省略了所有的制图内容,以作为数据处理阶段的证据。

检查
ACT-STATUS
列是否包含只需按
ACT
字典的键,然后检查列项目为
4,5A
(来自
MSW,C&T垃圾填埋场
记录)时会发生什么。您好,我找到了。谢谢。我重新编辑了我的帖子
import numpy as np
import matplotlib.pyplot as plt
from csv import reader

# the following two dicts are copied from your question
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'}
color = {'No Activity': 'black', 'CONTAMINATION CONFIRMED':'lightblue',
         'INVESTIGATION':'red', 'CORRECTIVE ACTION PLANNING':'pink',
         'IMPLEMENT ACTION':'yellow', 'MONITOR ACTION':'green', 'INACTIVE':'gray'}
# but we don't need ACT, we need its inverse…
ACT2codes = {}
for k, v in ACT.items(): ACT2codes.setdefault(v, []). append(k)

# ######################## let's read the data ########################
# lines is a generator, returns lines split on commas (respecting quoting)
# data is a dictionary of tuples of strings, indexed by the headers
lines = reader(open('hidalgo.csv', 'r'))    
data = {k:v for k, v in zip(next(lines), zip(*lines))}    
# but something it's better understood as an array of floats
for k in ('LONGITUDE',  'LATITUDE'):
    data[k] = np.array([float(item) for item in data[k]])        
# and something else is better understood as an array of strings,
# because we'll use np.where to find the indices required for plotting
data['ACT-STATUS'] = np.array(data['ACT-STATUS'])    
# ######################## ready to plot ########################
plt.figure(constrained_layout=1)
# for each action, plot some points with same color and same label
for action in ACT2codes.keys():
    # what are the indices of this batch of points?
    idx = []
    for code in ACT2codes[action]:
        idx += list(*np.where(data['ACT-STATUS']==code))
    plt.scatter(data['LONGITUDE'][idx],
                data['LATITUDE'][idx],
                color=color[action], label=action)
plt.legend() ; plt.show()