0

This is a small part of the data that needs to get in a barchart and the code I tried it with. But the parentheses and the "," instead of ":" within the parentheses make it that it is not possible in any ways I know how to get it done. ( I want to make a bar chart that shows the times a link shows like: '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)

And this is the error that comes with it:

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

Does anybody have an idea to make a bar chart with this type of data?

If I use the code that someone suggested in the comments I get an error and I think that the reason for that are the () around the links and the number, and beceause there are ',' instead of ':' between the keys and the values. Does anyone have a solution?

dennisvl
  • 1
  • 1
  • What exactly are you looking to plot? Your first key, for example, has a tuple as a value with 5 and 24 in there. – Kris Jan 19 '21 at 12:59
  • Does this answer your question? [Plot a bar using matplotlib using a dictionary](https://stackoverflow.com/questions/16010869/plot-a-bar-using-matplotlib-using-a-dictionary) – anurag Jan 19 '21 at 12:59
  • I want to plot the times the links are accuring. So 'http://www.wikidata.org/entity/Q31855' 5 times, 'http://www.wikidata.org/entity/Q5' 24 times ect. And with this code I get an error. I think because of the () end the , instead of a :. – dennisvl Jan 20 '21 at 10:22

1 Answers1

0

Reformatting the indentation of the dictionary shows:

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)]}

So, in this example there are only 2 keys with their corresponding values. The values are lists of tuples.

One way to plot such a dictionary could be to create one subplot per key, each subplot containing a bar plot of information present in the tuples.

As the strings are quite long, one could write a special-purpose function to split them. Another (or: additional) option would be to rotate the strings.

The following code shows an example to get you started:

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

JohanC
  • 71,591
  • 8
  • 33
  • 66