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()
map.colorbar()
I got error like this
TypeError: You must first set_array for mappable
Already read a lot materials but didn't see any fits my condition unfortunately.
I would be really thankful if any of you could help!
You have to plot via plt
like here:
...
sc=plt.scatter(x, y, 1, marker='o',color=color_lst)
plt.colorbar(sc)
...