根据州级数据制作加权的美国地图 [英] Making a weighted USA map based on state-level data
问题描述
我有一个州级数据列表,其中包含每个州的编号,例如:
I have a list of state-level data with numbers for each state, e.g.:
AL 10.5
AK 45.6
AZ 23.4
AR 15.0
...
我想将其制作为加权地图,数字最高时最暗,而最低处最低。是否有任何软件或Java或python库可以生成此类图像?
and I want to make it into a weighted map, with darkest where the number is highest and lightest here it is lowest. Is there any software, or a java or python library that can generate such an image?
推荐答案
如果要自己构建它,您只需要一张好的地图,该地图中每个州的位置集以及洪水填充。
If you want to build it yourself, all you need is a good map, a set of positions for each state in this map, and flood fill.
使用地图 http://www.clker。 com / cliparts / S / r / a / w / L / 0 / black-and-white-us-map-hi.png ,这是我们得到的:
Using the map http://www.clker.com/cliparts/S/r/a/w/L/0/black-and-white-u-s-map-hi.png, here is what we get:
import sys
from PIL import Image
def floodfill(img, seed, color):
im = img.load()
work = [seed]
start_color = im[seed]
while work:
x, y = work.pop()
im[x, y] = color
for dx, dy in ((-1,0), (1,0), (0,-1), (0,1)):
nx, ny = x + dx, y + dy
if im[nx, ny] == start_color:
work.append((nx, ny))
USA_MAP = Image.open(sys.argv[1]).convert('1')
POINT_STATE = {'AL': (420, 260), 'AZ': (110, 240), 'AR': (350, 250)}
painted_map = USA_MAP.convert('L')
data = {'AL': 10.5, 'AZ': 23.4, 'AR': 15.0}
# Normalize data based on the minimum weight being 0+eps and maximum 30.
for k, v in data.items():
v = v/30.
color = int(round(255 * v))
floodfill(painted_map, POINT_STATE[k], 255 - color)
painted_map.save(sys.argv[2])
这篇关于根据州级数据制作加权的美国地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!