我正在使用pygal制作一张显示2010年世界各国人口的交互式地图。我正在设法使这个国家的人口用逗号显示,即10,000,而不仅仅是10000。

我已经尝试使用"{:,}".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')发布于 2019-03-30 18:48:14
设置value_formatter属性将更改标签格式,但在您的代码中,您可以在设置属性之后重新创建World对象。这个新创建的对象将具有默认值格式化程序。您还可以删除设置value_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')https://stackoverflow.com/questions/55428559
复制相似问题