Use Python to make simple data visualizations yourself

Source: Internet
Author: User

Use only Python's random library to generate html-formatted label clouds for existing Data. The idea is to generate a view of the data for different sizes of different color words depending on how many times the same word Appears.

For example, multiple data in the following format:

1gaming1skateboarding2girl friend3surfing the internet3ted talks4reading4writing5facebook5gaming6gaming6martial Arts7partying7playing sport7travel8driving8socializing with friends9eating9procrastinating9sleeping10winning ...

Can be produced as Follows:

first, the data exists in a dict, the key is the word, the value is the number of occurrences:

Words = "in data:    word = line.split (' \ t ') [1]    If Word not in words:        words[word] = 1    else:        wor ds[word] + = 1

You will then make html, set different words to random colors, and set different font sizes by the frequency at which the words appear.

html = "" for w, c in words.items ():    color = ' RGB (%s,%s,%s) '% (str (random.randint (0, 255)), str (random.randint (0, 25 5)), str (random.randint (0, 255)))    fontsize = int (c * 0.1 +)    html + = ' <span style=\ ' font-size: ' + str (fontsiz E) + ' px;color: ' + color + '; float:left;\ ' > ' + w + ' </span> ' # dump it to a filewith open (' result.html ', ' WB ') as F:    f.write (bytes (html, ' UTF-8 '))

here, It's done!

Use Python to make simple data visualizations yourself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.