2

the sample file looks like thisx-axis should have alphabets ranging from a-z+A-Z and y-axis should plot their respective frequencies from content column

import pandas as pd
import numpy as np
import string
from matplotlib import pyplot as plt
plt.style.use('fivethirtyeight')

col_list = ["tweet_id","sentiment","author","content"]
df = pd.read_csv("sample.csv",usecols=col_list)
freq = (df["content"])

frequencies = {}

for sentence in freq:
    for char in sentence:
        if char in frequencies:
            frequencies[char] += 1
        else:
            frequencies[char] = 1

frequency = str(frequencies)

bins = [chr(i + ord('a')) for i in range(26)].__add__([chr(j + ord('A')) for j in range(26)])


plt.title('data')
plt.xlabel('letters')
plt.ylabel('frequencies')
plt.hist(bins,frequency,edgecolor ='black')
plt.tight_layout()

plt.show()
6
  • Could you please provide some sample data? How bins and frequency look like? Commented Jul 3, 2020 at 11:51
  • From what you are describing you don't want a histogram but a bar chart.
    – Darina
    Commented Jul 3, 2020 at 11:58
  • added an image for the data Commented Jul 3, 2020 at 12:43
  • @Darina do we have to use some other function other than plt.hist() to plot a bar chart?? Commented Jul 3, 2020 at 12:46
  • @MridulSetia yes, there is both a matplotlib function and a pandas wraparound for it. Just google "python barplot".
    – Darina
    Commented Jul 3, 2020 at 15:23

1 Answer 1

2

Your code is already well structured, still I would suggest using plt.bar with letters on xticks as opposed to plt.hist, as it seems easier to work with chars on x axis. I commented the else so that nothing apart from the desired letters (a-zA-Z) would be added. Also included a sorted command to give the option of having the bars sorted alphabetically or by frequency count.

Input used in sample.csv

    tweet_id  sentiment  author                                            content
0        NaN        NaN     NaN  @tiffanylue i know i was listenin to bad habit...
1        NaN        NaN     NaN  Layin n bed with a headache ughhhh...waitin on...
2        NaN        NaN     NaN                Funeral ceremony...gloomy friday...
3        NaN        NaN     NaN               wants to hang out with friends SOON!
4        NaN        NaN     NaN  @dannycastillo We want to trade with someone w...
5        NaN        NaN     NaN  Re-pinging @ghostridahl4: why didn't you go to...
6        NaN        NaN     NaN  I should be sleep, but im not! thinking about ...
...
...
# populate dictionary a-zA-Z with zeros
frequencies = {}
for i in range(26):
    frequencies[chr(i + ord('a'))] = 0
    frequencies[chr(i + ord('A'))] = 0

# iterate over each row of "content"
for row in df.loc[:,"content"]:
    for char in row:
        if char in frequencies:
            frequencies[char] += 1
        # uncomment to include numbers and symbols (!@#$...)
        # else:
        #     frequencies[char] = 1

# sort items from highest count to lowest
char_freq = sorted(frequencies.items(), key=lambda x: x[1], reverse=True)
# char_freq = sorted(frequencies.items(), key=lambda x: x, reverse=False)

plt.title('data')
plt.xlabel('letters')
plt.ylabel('frequencies')

plt.bar(range(len(char_freq)), [i[1] for i in char_freq], align='center')
plt.xticks(range(len(char_freq)), [i[0] for i in char_freq])

plt.tight_layout()

plt.show()

sorted_alphabetically sorted_by_count

Not the answer you're looking for? Browse other questions tagged or ask your own question.