2016-08-06 31 views
-2

Liste der am häufigsten verwendeten Wörter Output unten gegeben:Grundstück häufigsten Wörter in Python

[('Film', 904), ('Film', 561), ('one', 379), ('wie', 292)]

möchte ich ein Diagramm für jedes Wort mit matplotlib nach den Zahlen

bitte helfen Sie mir

+1

Welchen Code haben Sie bisher? – Kosch

+0

Eigentlich habe ich versucht, Matplotlib auf verschiedene Arten zu verwenden. Aber ich fand Lösung für die Eingabe wie x = [a, b, c] und y = [1, 3,4] oder eine andere Art wie für eine Tabelle. Aber ich habe die Lösung für die Ausgabe nicht wie oben beschrieben bekommen. – RokiDGupta

+0

Habe ich das richtig verstanden: Sie wissen, wie es geht, wenn Sie zwei Listen haben und jetzt nach einer Möglichkeit suchen, zwei Listen aus Ihren Daten zu erstellen? – Matthias

Antwort

2

Hier ist eine schnelle adoptieren dieser example ein Balkendiagramm mit.

#!/usr/bin/env python 
# a bar plot with errorbars 
import numpy as np 
import matplotlib.pyplot as plt 


data = [('film', 904), ('movie', 561), ('one', 379), ('like', 292)] 
names, values = zip(*data) # @comment by Matthias 
# names = [x[0] for x in data] # These two lines are equivalent to the the zip-command. 
# values = [x[1] for x in data] # These two lines are equivalent to the the zip-command. 

ind = np.arange(len(data)) # the x locations for the groups 
width = 0.35  # the width of the bars 

fig, ax = plt.subplots() 
rects1 = ax.bar(ind, values, width, color='r') 

# add some text for labels, title and axes ticks 
ax.set_ylabel('Count') 
ax.set_xticks(ind+width/2.) 
ax.set_xticklabels(names) 



def autolabel(rects): 
    # attach some text labels 
    for rect in rects: 
     height = rect.get_height() 
     ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
       '%d' % int(height), 
       ha='center', va='bottom') 

autolabel(rects1) 

plt.show() 
+0

'Namen' und' Werte' können mit ['zip'] erstellt werden (https://docs.python.org/3/library/functions .html # zip): 'name, values ​​= zip (* data)' – Matthias

+0

Dachte nicht, danke, dass du mich erinnert hast! Das ist in der Tat süßer. – pathoren

+0

Danke, es funktioniert gut !!! – RokiDGupta

1

können Sie versuchen, diese:

""" 
Bar chart demo with pairs of bars grouped for easy comparison. 
""" 
import numpy as np 
import matplotlib.pyplot as plt 

data = [('film', 904), ('movie', 561), ('one', 379), ('like', 292)] 

n_groups = len(data) 

vals_films = [x[1] for x in data] 
legends_films = [x[0] for x in data] 

fig, ax = plt.subplots() 

index = np.arange(n_groups) 
bar_width = 0.25 

opacity = 0.4 

rects1 = plt.bar(index, vals_films, bar_width, 
       alpha=opacity, 
       color='b', 
       label='Ocurrences') 


plt.xlabel('Occurrences') 
plt.ylabel('Words') 
plt.title('Occurrences by word') 
plt.xticks(index + bar_width, legends_films) 
plt.legend() 

plt.tight_layout() 
plt.show() 

Wenn Sie Jupyter Notebook verwenden (empfohlen) geschehen, fügen Sie diese zu Beginn des Notebooks: %matplotlib notebook

+0

Ja, es funktioniert auch ... und danke für Ihren Vorschlag .. – RokiDGupta

Verwandte Themen