【问题标题】:How do I display text in a Pygal map?如何在 Pygal 地图中显示文本?
【发布时间】:2022-12-06 19:05:26
【问题描述】:

我这里的问题是,当你点击每个国家时,我想显示一点文本,比如每个国家的首都。现在,我只包括了占位符数字。我尝试使用 str("x") 命令,但没有成功。我在这里做什么?

这是我目前拥有的屏幕截图,在我想要的解决方案中,我希望显示“华盛顿特区”而不是 66。 Here is the image result I am getting

# import pygal library
import pygal
mm = pygal.maps.world.World()
# create a world map
worldmap = pygal.maps.world.World()
# set the title of the map
worldmap.title = 'Countries'
# adding the countries
worldmap.add('Random Data', {
        'aq' : 69,
        'cd' : 30,
        'de' : 40,
        'eg' : 50,
        'ga' : 45,
        'hk' : 23,
        'in' : 0,
        'jp' : 65,
        'nz' : 41,  
        'kz' : 32,
        'us' : 66
})
# save into the file
worldmap.render_to_file('worldmapresult.svg')
open('worldmapresult.svg')
print("Success")

【问题讨论】:

    标签: python string integer pygal


    【解决方案1】:

    您可以使用 pygal 中的tooltip_...Here 是所有与工具提示相关的文档。

    为工具提示添加字体大小和边框半径:

    worldmap.tooltip_font_size = 14
    worldmap.tooltip_border_radius = 10
    

    现在添加适当的标签:

    worldmap.value_formatter = lambda x: {
        'aq': '', # does it have a capital?
        'cd': 'Kinshasa',
        'de': 'Berlin',
        'eg': 'Cairo',
        'ga': 'Libreville',
        'hk': 'Hong Kong',
        'in': 'New Delhi',
        'jp': 'Tokyo',
        'nz': 'Wellington',
        'kz': 'Astana',
        'us': 'Washington DC'
    }[x]
    

    这是完整的代码:

    # import pygal library
    import pygal
    
    # create a world map
    worldmap = pygal.maps.world.World()
    
    # set the title of the map
    worldmap.title = 'Countries'
    
    worldmap.tooltip_font_size = 14
    worldmap.tooltip_border_radius = 10
    
    worldmap.value_formatter = lambda x: {
        'aq': '',
        'cd': 'Kinshasa',
        'de': 'Berlin',
        'eg': 'Cairo',
        'ga': 'Libreville',
        'hk': 'Hong Kong',
        'in': 'New Delhi',
        'jp': 'Tokyo',
        'nz': 'Wellington',
        'kz': 'Astana',
        'us': 'Washington DC'
    }[x]
    
    # adding the countries
    worldmap.add('Random Data', {
            'aq' : 69,
            'cd' : 30,
            'de' : 40,
            'eg' : 50,
            'ga' : 45,
            'hk' : 23,
            'in' : 0,
            'jp' : 65,
            'nz' : 41,  
            'kz' : 32,
            'us' : 66
    })
    
    # save into the file
    worldmap.render_to_file('worldmapresult.svg')
    open('worldmapresult.svg')
    print("Success")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多