【问题标题】:Python3 Pygal Worldmap Data not showingPython3 Pygal Worldmap 数据未显示
【发布时间】:2018-06-16 12:00:47
【问题描述】:

将 json 文件加载到列表中,转换为字典以加载到 pygal 世界地图中。当我打印字典时,数据看起来不错(对我来说),但是地图会打开(来自 svg 文件)但没有绘制数据。

没有发生回溯错误。

我没有运行最新版本的 pygal。

字典的示例输出:

{'AF': 9733784, 'AL': 1437590, 'DZ': 92215683, 'AO': 17394550, 'AG': 19061, 'AR': 0}

代码如下:

import json

import pygal

# Load the data into a list.

filename = 'test.json'
with open(filename, 'rb') as f:
    sr_data = json.load(f)

# Print sr_data rows.

sr_exp = {}
for sr_dict in sr_data:
    country_code = sr_dict['CountryCode']
    gross = int(float(sr_dict['Exposed']))
    if country_code:
        sr_exp[country_code] = gross

# Create map.

wm = pygal.Worldmap()
wm.title = 'SR Data'
wm.add('',sr_exp)

wm.render_to_file('sr.svg')

【问题讨论】:

    标签: python-3.x pygal


    【解决方案1】:

    在您的字典中用作键的国家/地区代码需要小写。最简单的解决办法是换行

    sr_exp[country_code] = gross
    

    sr_exp[country_code.lower()] = gross
    

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 2016-07-19
      相关资源
      最近更新 更多