laserson / squarify

Pure Python implementation of the squarify treemap layout algorithm
Other
298 stars 37 forks source link

TypeError: unsupported operand type(s) for +: 'int' and 'str' #28

Closed spouyllau closed 3 years ago

spouyllau commented 3 years ago

Hello, With squarify, I try to do an treemap but I have an :

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-142-f809b7b5caff> in <module>
     40 
     41 # Plot
---> 42 squarify.plot(sizes='data' , label='labels' , alpha=.7)
     43 plt.axis('off')
     44 plt.show()

/opt/jupyterhub/lib/python3.7/site-packages/squarify/__init__.py in plot(sizes, norm_x, norm_y, color, label, value, ax, pad, bar_kwargs, text_kwargs, **kwargs)
    239         bar_kwargs.update(kwargs)
    240 
--> 241     normed = normalize_sizes(sizes, norm_x, norm_y)
    242 
    243     if pad:

/opt/jupyterhub/lib/python3.7/site-packages/squarify/__init__.py in normalize_sizes(sizes, dx, dy)
    168         The normalized values.
    169     """
--> 170     total_size = sum(sizes)
    171     total_area = dx * dy
    172     sizes = map(float, sizes)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

My code is :

from SPARQLWrapper import SPARQLWrapper, JSON
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import re

sparql = SPARQLWrapper("http://isidore.science/sparql")
sparql.setQuery("""
PREFIX sioc: <http://rdfs.org/sioc/ns#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX ore: <http://www.openarchives.org/ore/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?label (count(?documents) as ?count) WHERE {
    <http://isidore.science/collection/10670/2.qfy8eq> ore:aggregates ?documents.
    ?documents sioc:topic ?topic.
    ?topic skos:prefLabel ?label.
    FILTER(lang(?label)="fr")
}
GROUP BY ?label
ORDER BY DESC(?count) LIMIT 10

""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
results_df = pd.json_normalize(results['results']['bindings'])
labels = results_df['label.value']
data = results_df['count.value']

# Dataframe
df = pd.DataFrame({'data':data, 'labels':labels})
print(df)

# Tree
squarify.plot(sizes='data' , label='labels' , alpha=.7)
plt.axis('off')
plt.show()

What's wrong with this data in a DataFrame ? Best, Stéphane.

laserson commented 3 years ago

You can see that the first argument to plot() must be sizes, which should be a list of sizes (i.e., that input you would normally give to the squarify function itself.

In your code, you provided sizes with a string sizes="data". You probably meant to do sizes=df["data"]. Feel free to reopen if that doesn't solve the issue.