我如何想象用括号和逗号代替冒号的词汇表

2024-10-01 07:50:28 发布

您现在位置:Python中文网/ 问答频道 /正文

这是需要在条形图中获取的一小部分数据,以及我尝试使用的代码。但是括号和括号内的“,”而不是“:”,使我知道如何完成这项工作在任何方面都是不可能的。(我想制作一个条形图,显示链接显示的时间,如:'http://www.wikidata.org/entity/Q31855 5, 'http://www.wikidata.org/entity/Q5",24岁)

a_dictionary = {'class vs. total number of instances of the class': [('http://www.wikidata.org/entity/Q31855',
   5),
  ('http://www.wikidata.org/entity/Q5', 24),
  ('http://www.wikidata.org/entity/Q9388534', 25)],
 'property vs. total number of distinct objects in triples using the property': [('http://www.wikidata.org/prop/direct/P800',
   1),
  ('https://w3id.org/artchives/wikidataReconciliation', 1),
  ('http://www.w3.org/ns/prov#wasInfluencedBy', 2),
  ('https://w3id.org/artchives/publicationStage', 2),
  ('https://w3id.org/artchives/hasSecondLink', 2)]}


keys = a_dictionary.keys()
values = a_dictionary.values()

plt.bar(keys, values)

这就是随之而来的错误:

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U39') dtype('<U39') dtype('<U39')

有人想到用这种数据制作条形图吗

如果我使用有人在评论中建议的代码,我会得到一个错误,我认为原因是链接和数字周围的(),因为键和值之间有“,”而不是“:”。有人有解决办法吗


Tags: ofhttpsorghttpdictionarywwwkeysentity
1条回答
网友
1楼 · 发布于 2024-10-01 07:50:28

重新设置词典缩进的格式显示:

a_dictionary = {'class vs. total number of instances of the class':
                    [('http://www.wikidata.org/entity/Q31855', 5),
                     ('http://www.wikidata.org/entity/Q5', 24),
                     ('http://www.wikidata.org/entity/Q9388534', 25)],
                'property vs. total number of distinct objects in triples using the property':
                    [('http://www.wikidata.org/prop/direct/P800', 1),
                     ('https://w3id.org/artchives/wikidataReconciliation', 1),
                     ('http://www.w3.org/ns/prov#wasInfluencedBy', 2),
                     ('https://w3id.org/artchives/publicationStage', 2),
                     ('https://w3id.org/artchives/hasSecondLink', 2)]}

因此,在本例中,只有两个键及其相应的值。这些值是元组列表

绘制这样一个字典的一种方法是为每个键创建一个子图,每个子图包含元组中存在的信息的条形图

由于字符串很长,可以编写一个专用函数来拆分它们。另一个(或:附加)选项是旋转字符串

下面的代码显示了一个开始的示例:

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

def split_url(url_string):
    splitted = url_string.split('/')
    res = ''
    length = 0
    for s in splitted[:-1]:
        length += len(s)
        if length < 9:
            res += s + '/'
        else:
            res += s + '/\n'
            length = 0
    res += '\n#'.join(splitted[-1].split('#'))
    return res

fig, axes = plt.subplots(ncols=len(a_dictionary), figsize=(20, 5),
                         gridspec_kw={'width_ratios': [len(v) for v in a_dictionary.values()]})
for (key, occurrences), ax in zip(a_dictionary.items(), axes):
    ax.bar([split_url(url) for url, num in occurrences],
           [num for label, num in occurrences], color='turquoise')
    ax.set_title(key)
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
    # ax.tick_params(axis='x', rotation=90)
plt.tight_layout()
plt.show()

example plot

相关问题 更多 >