我想根据散景的map of US unemployment制作一张chloropleth地图,我对这个图书馆并不是很熟悉,但我对熊猫有些熟悉。我有一个国家数据框和我们系统的优先级。我无法获得正确显示的优先级。我认为我错误地使用了fill_color属性,但除了散景文档中列出的one line之外,我找不到关于它的更多信息。散热图不显示正确的结果
我的预期输出是针对高优先级显示为红色,中等优先级显示为绿色,低优先级显示为黄色。 (颜色方案不是我的选择。)如果状态没有优先级,它们应该显示为白色。然而,输出并不符合 - 颜色似乎是随机放置的。一些低优先级的国家是红色的,一些是黄色的;一些中等优先的国家是白色的,有些是红色的;等等
我想错误在于我的state_colors
列表。现在我正在循环并更新每个状态的优先级,但由于某种原因它没有正确更新。有没有办法在patch()方法内更改fill_color
属性,以便颜色对应于优先级值?或者我只是搞乱state_colors
名单?
from bokeh.models import ColumnDataSource
from bokeh.sampledata import us_states
from bokeh.plotting import figure, show, output_file
import pandas as pd
df2 = pd.read_excel(filename, sheetname='2012 Master', header=1, skip_footer=4, parse_cols='A,B,L,P')
usa_states = us_states.data.copy()
state_xs = [usa_states[code]["lons"] for code in usa_states]
state_ys = [usa_states[code]["lats"] for code in usa_states]
df2.fillna(value="", inplace=True)
state_colors = []
for state in df2["State"]:
for priority in df2["Priority"]:
if priority == "High":
state_colors.append("#FF0000")
elif priority == "Medium":
state_colors.append("#008000")
elif priority == "Low":
state_colors.append("#FFFF00")
else:
state_colors.append("#FFFFFF")
source = ColumnDataSource(
data=dict(
st = df2['State'],
priority = df2['Priority']
)
)
p = figure(title="Title", tools= TOOLS, toolbar_location="above", plot_width=1100, plot_height=700, x_range=[-128,-64], y_range=[23,50])
p.patches(state_xs, state_ys, fill_color = state_colors, source=source, fill_alpha=1, line_color="black", line_width=1)
hover = p.select(dict(type=HoverTool))
hover.tooltips= [
("State","@st"),
("Priority","@priority"),
]
show(p)
我的数据帧(DF2):
State Priority
Alabama Low
Arizona
Arkansas
California Medium
Colorado
Connecticut
Delaware
District of Columbia
Florida High
Georgia Medium
Idaho High
Illinois Low
...
Wisconsin High
Wyoming Low
我使用熊猫0.17.1。 Here是散景使用的usa_states代码。 Here是我当前的输出。