Je cherche depuis des heures, mais je ne trouve tout simplement pas la solution à mon problème.
J'ai un dictionnaire:
TypeError Traceback (most recent call last)
<ipython-input-258-7ca72d2d0ca4> in <module>
1 # dont use values inside a list as column values for a dataframe
2 browser2 = {}
----> 3 [browser2.update({key : val[0]}) for key, val in amount_of_TP_ner_tags.items()]
4
5 x = pd.Series(browser2)
<ipython-input-258-7ca72d2d0ca4> in <listcomp>(.0)
1 # dont use values inside a list as column values for a dataframe
2 browser2 = {}
----> 3 [browser2.update({key : val[0]}) for key, val in amount_of_TP_ner_tags.items()]
4
5 x = pd.Series(browser2)
TypeError: 'int' object is not subscriptable
Tout ce que je veux, c'est obtenir ceci est représenté dans un graphique à secteurs qui montre la répartition en pourcentage de ces éléments. Comment puis-je tracer ça ????
J'ai essayé la solution ici: Essayer de créer un camembert et un histogramme à partir des données ci-dessous
J'ai donc utilisé le code qui y a été publié comme solution :
amount_of_TP_ner_tags # this is the dictionary I want to plot. see above
# dont use values inside a list as column values for a dataframe
browser2 = {}
[browser2.update({key : val[0]}) for key, val in amount_of_TP_ner_tags.items()]
x = pd.Series(browser2)
y = pd.Series.sort_values(x)
z = pd.DataFrame(y)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8,4))
# you need to specify which column of the dataframe you want to plot (here 0)
z.plot(y=0, kind = 'pie', ax = axes[0])
z.plot(kind = 'bar', ax = axes[1])
axes[0].set_aspect("equal") # make aspect equal (such that circle is not eliptic)
#place the legend at a decent position
axes[0].legend(loc=1, bbox_to_anchor= (1,0.2), fontsize=11)
plt.tight_layout()
plt.show()
Mais je reçois un message d'erreur:
{'DATE': 65,
'ORG': 93,
'PERSON': 62,
'GPE': 18,
'PRODUCT': 18,
'FAC': 4,
'CARDINAL': 50,
'ORDINAL': 15,
'NORP': 10,
'MONEY': 3,
'PERCENT': 2,
'TIME': 5,
'LOC': 5,
'QUANTITY': 2}
Alors, comment puis-je tracer cela?! J'ai vraiment besoin d'aide, s'il vous plaît.
Cordialement
4 Réponses :
Le moyen le plus simple consiste à utiliser le camembert matplotlib
import matplotlib.pyplot as plt
your_data = {'DATE': 65,
'ORG': 93,
'PERSON': 62,
'GPE': 18,
'PRODUCT': 18,
'FAC': 4,
'CARDINAL': 50,
'ORDINAL': 15,
'NORP': 10,
'MONEY': 3,
'PERCENT': 2,
'TIME': 5,
'LOC': 5,
'QUANTITY': 2}
# Data to plot
labels = []
sizes = []
for x, y in your_data.items():
labels.append(x)
sizes.append(y)
# Plot
plt.pie(sizes, labels=labels)
plt.axis('equal')
plt.show()
Votre erreur ne concerne pas le traçage, vous essayez d'accéder à l'index d'un entier.
Essayez ceci:
browser2 = {}
[browser2.update({key : val}) for key, val in amount_of_TP_ner_tags.items()]
C'est très simple
Étape 1: Extraire les étiquettes et les valeurs du dictionnaire
import matplotlib.pyplot as plt plt.pie(values, labels=labels) plt.show()
Étape 2: Appelez simplement le matplotlib.pyplot ' méthode de tarte.
# Get the Keys and store them in a list labels = list(yourData.keys()) # Get the Values and store them in a list values = list(yourData.values())
Et vous serez prêt à partir.
import matplotlib.pyplot as plt
your_data = {'DATE': 65,
'ORG': 93,
'PERSON': 62,
'GPE': 18,
'PRODUCT': 18,
'FAC': 4,
'CARDINAL': 50,
'ORDINAL': 15,
'NORP': 10,
'MONEY': 3,
'PERCENT': 2,
'TIME': 5,
'LOC': 5,
'QUANTITY': 2}
plt.pie([float(your_data[v]) for v in your_data], labels=[str(k) for k in your_data], autopct='%1.1f%%')
plt.show()