1

Hi guys i want to plot histogram in seaborn from dictionary data

{'C873-': 15, 'C860T': 16, 'A1860-': 17, 'A1878-': 17, 'C652T': 20, 'G372-': 33, 'C2107T': 37, '-447A': 144}

there is couple of threads with similar questions but they are saying that bar plot is better or given answers don't work as they shoudl. I need to plot this as histogram

what i was trying to do is

plt.hist([i[-1] for i in sum_dict],  bins=len(sum_dict), color='g')
plt.show()

but this looks awful, is in plotly and probably is wrong

Im not stubborn to use seaborn so i will check out everything

newDev2000
  • 271
  • 3
  • 12
  • I'm not clear on what are you trying to achieve. What are the contents of the `sum_dict`? – LemurPwned Feb 08 '21 at 10:22
  • 2
    Are you sure you want a histogram and not a bar plot? – Diziet Asahi Feb 08 '21 at 10:28
  • 1
    Why do you have to plot this as a histogram? A histogram of n values in n bins is called a bar plot, even more so, since your x-values are categorical data. – Mr. T Feb 08 '21 at 10:32
  • @LemurPwned i posted values of sum dict, As i said i need to plot this data as histogram becouse that is my task. This dict is made from list of string so if ploting from dict is a trouble i we can plot this from list – newDev2000 Feb 08 '21 at 12:12
  • @newDev2000 if that's the case, then the above suggestions are correct -- you should use `plt.bar` over `plt.hist` since you already have the value counts. – LemurPwned Feb 08 '21 at 12:35

1 Answers1

2

You should want to use plt.bar instead of plt.hist, since you already have the counts of each value (it's still a histogram though).

import matplotlib.pyplot as plt
import seaborn as sns

data = {
    'C873-': 15,
    'C860T': 16,
    'A1860-': 17,
    'A1878-': 17,
    'C652T': 20,
    'G372-': 33,
    'C2107T': 37,
    '-447A': 144
}

ind = np.arange(len(data))

plt.bar(ind, list(data.values()))
plt.xticks(ind, list(data.keys()))
plt.show()

One cool thing is that you may use the seaborn colour palette along with the code above, simply by passing a colour range generated by sns.color_palette to plt.bar function's color argument:

import matplotlib.pyplot as plt
import seaborn as sns

data = {
...
}

ind = np.arange(len(data))
palette = sns.color_palette("husl", len(data))

plt.bar(ind, list(data.values()), color=palette)
plt.xticks(ind, list(data.keys()))
plt.show()

And the final result: enter image description here

LemurPwned
  • 710
  • 10
  • 19