Python 在使用pygal.maps.world时,是否有方法格式化显示国家的数字';人口是多少?
我正在用pygal制作一张显示2010年世界各国人口的互动地图。我正试图找到一种方法,让这个国家的人口显示为插入逗号的10000人,而不仅仅是10000人 我已经尝试使用“{:,}”.format(x)将不同人口级别的数字读入我的列表,但它会导致错误。我认为这是因为它将值更改为字符串 我还试着插入一段在网上找到的代码Python 在使用pygal.maps.world时,是否有方法格式化显示国家的数字';人口是多少?,python,number-formatting,pygal,Python,Number Formatting,Pygal,我正在用pygal制作一张显示2010年世界各国人口的互动地图。我正试图找到一种方法,让这个国家的人口显示为插入逗号的10000人,而不仅仅是10000人 我已经尝试使用“{:,}”.format(x)将不同人口级别的数字读入我的列表,但它会导致错误。我认为这是因为它将值更改为字符串 我还试着插入一段在网上找到的代码 wm.value_formatter = lambda x: "{:,}".format(x). 这不会导致任何错误,但也无法修复数字的格式。我希望有人知道一个内置功能,例如
wm.value_formatter = lambda x: "{:,}".format(x).
这不会导致任何错误,但也无法修复数字的格式。我希望有人知道一个内置功能,例如:
wm_style = RotateStyle('#336699')
这让我可以设定一个配色方案
下面是我绘制地图代码的一部分
wm = World()
wm.force_uri_protocol = "http"
wm_style = RotateStyle('#996699')
wm.value_formatter = lambda x: "{:,}".format(x)
wm.value_formatter = lambda y: "{:,}".format(y)
wm = World(style=wm_style)
wm.title = "Country populations year 2010"
wm.add('0-10 million', cc_pop_low)
wm.add("10m to 1 billion", cc_pop_mid)
wm.add('Over 1 billion', cc_pop_high)
wm.render_to_file('world_population.svg')
设置
value\u formatter
属性将更改标签格式,但在代码中,您可以在设置属性后重新创建World
对象。这个新创建的对象将具有默认值格式化程序。您还可以删除设置value\u formatter
属性的其中一行,因为它们实现了相同的功能
重新订购代码将解决您的问题:
wm_style = RotateStyle('#996699')
wm = World(style=wm_style)
wm.value_formatter = lambda x: "{:,}".format(x)
wm.force_uri_protocol = "http"
wm.title = "Country populations year 2010"
wm.add('0-10 million', cc_pop_low)
wm.add("10m to 1 billion", cc_pop_mid)
wm.add('Over 1 billion', cc_pop_high)
wm.render_to_file('world_population.svg')
工作得很好。谢谢你解释我的错误。