在底图中显示离散颜色栏

2024-09-29 19:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

import csv from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import matplotlib import numpy as np map = Basemap(projection='stere',llcrnrlat=21.5, urcrnrlat=26, llcrnrlon=119.5, urcrnrlon=122.5, resolution='l', lat_0 = 22, lon_0=121) map.drawcoastlines(linewidth=0.25) map.drawcountries(linewidth=0.25) map.drawcoastlines() map.drawstates() map.drawcountries() class House: def __init__(self, num, x, y): self.num = num self.x = float(x) self.y = float(y) lons = [] lats = [] color_lst = [] print(lons) with open('/Users/yan/Downloads/tw-rental-data/2018Q3-raw-01.csv', newline='') as csvfile: rows = csv.DictReader(csvfile) for row in rows: if row['約略地點_x'] != '-' and row['約略地點_y'] != '-' and row['物件類型'] == '1': h = House(row['物件編號'], row['約略地點_x'], row['約略地點_y']) lons.append(h.y) lats.append(h.x) #print(row['物件編號'], row['約略地點_x'], row['約略地點_y']) #print(h.x) #map.scatter(h.x, h.y, 1, marker='o', color='k') if int(row['月租金']) < 4000: color_lst.append('lightcyan') elif 4000 <= int(row['月租金']) < 5000: color_lst.append('powderblue') elif 5000 <= int(row['月租金']) < 6000: color_lst.append('lightblue') elif 6000 <= int(row['月租金']) < 7000: color_lst.append('lightskyblue') elif 7000 <= int(row['月租金']) < 8000: color_lst.append('deepskyblue') elif 8000 <= int(row['月租金']) < 9000: color_lst.append('royalblue') elif 9000 <= int(row['月租金']) < 10000: color_lst.append('mediumblue') else: color_lst.append('midnightblue') #print(lons) x, y = map(lons, lats) map.scatter(x, y, 1, marker='o',color=color_lst) map.colorbar() #plt.colorbar(x) plt.show()
enter image description here 我已经用上面的代码得到了这张图片,但是我仍然想添加一个冒号栏来显示我的底图上每种颜色的含义。它们都是离散的,但从浅到深都在变化。 当我这么做的时候

map.colorbar()

我犯了这样的错误

TypeError: You must first set_array for mappable

已经读了很多资料,但没有看到任何适合我的条件不幸。你知道吗

如果你们能帮上忙,我会非常感激的!你知道吗


Tags: csvimportselfmapascolorintrow

热门问题