Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to generate word-frequency plot w/ Python?

Tags:

python

plot

I have a file that contains a word and the frequency that it occurs. I would like to generate a sort of plot; I'm looking for a sort of 'bubble' like graph. The idea is that the size of these bubbles corresponds to the relative frequencies and the corresponding word is labeled on these bubbles. Does anyone know if this can be done with the standard matplotlib or anything similar?

like image 475
Ayumu Kasugano Avatar asked Oct 18 '25 01:10

Ayumu Kasugano


1 Answers

There are lots of libraries out there.

Here's an example from WordCloud

#!/usr/bin/env python
"""
Minimal Example
===============
Generating a square wordcloud from the US constitution using default arguments.
"""

import os

from os import path
from wordcloud import WordCloud

# using word frequency list:
#word_freq = open("/tmp/word_freq.txt").read()
# say it looks like this:
word_freq = {'apple': 4, 'banana': 1, 'melon': 2, 'strawberry': 3, 'grape': 8}
text = " ".join([(k + " ")*v for k,v in word_freq.items()])

# Generate a word cloud image
wordcloud = WordCloud().generate(text)


# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")

# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()

WordCloud from different text: wordcloud

like image 97
keithpjolley Avatar answered Oct 20 '25 14:10

keithpjolley