Seite 1 von 1

Matplotlib

Verfasst: Dienstag 31. Oktober 2017, 22:55
von Iveto
Hallo zusammen,

kann mir jemand helfen? Ich versuche zu verstehen, wie man mit Hilfe von Matplotlib sprachliche Daten abbilden kann. Ich benutze das Buch Natural Language Processing with Python http://www.nltk.org/book/ch04.html und dort ist im Kapitel 4 ein Beispiel benannt. Es wird nämlich die Sequenz/das Vorkommen von Modalverben aus einem Sprachkorpus ausgewertet und graphisch abgebildet.

Die Datei sieht nämlich so aus:

from numpy import arange
from matplotlib import pyplot

colors = 'rgbcmyk' # red, green, blue, cyan, magenta, yellow, black

def bar_chart(categories, words, counts):
"Plot a bar chart showing counts for each word by category"
ind = arange(len(words))
width = 1 / (len(categories) + 1)
bar_groups = []
for c in range(len(categories)):
bars = pyplot.bar(ind+c*width, counts[categories[c]], width,
color=colors[c % len(colors)])
bar_groups.append(bars)
pyplot.xticks(ind+width, words)
pyplot.legend([b[0] for b in bar_groups], categories, loc='upper left')
pyplot.ylabel('Frequency')
pyplot.title('Frequency of Six Modal Verbs by Genre')
pyplot.show()

und angeblich soll man im IDLE folgendes eingeben:


>>> genres = ['news', 'religion', 'hobbies', 'government', 'adventure']
>>> modals = ['can', 'could', 'may', 'might', 'must', 'will']
>>> cfdist = nltk.ConditionalFreqDist(
... (genre, word)
... for genre in genres
... for word in nltk.corpus.brown.words(categories=genre)
... if word in modals)
...
>>> counts = {}
>>> for genre in genres:
... counts[genre] = [cfdist[genre][word] for word in modals]
>>> bar_chart(genres, modals, counts)


Kennt sich jemand damit aus? Bei mir funktioniert es nicht, egal was ich versuche.

Re: Matplotlib

Verfasst: Mittwoch 1. November 2017, 20:26
von narpfel
Moin,

„es funktioniert nicht“ ist keine Fehlerbeschreibung, mit der man sinnvoll etwas anfangen könnte. Was genau versuchst du, was erwartest du und was ist das Ergebnis? Falls es eine Fehlermeldung gibt, zeige bitte den gesamten Traceback.

Bei mir (Python 3.6 und nltk 3.2.5) erzeugt der Code ohne Fehlermeldung ein Histogramm.

Re: Matplotlib

Verfasst: Mittwoch 1. November 2017, 20:35
von pixewakb
Oben links, wo "Select code" steht, kann man eine Code-Box für Python auswählen, alles andere ist für die anderen nicht gut...