Anderson Silva Anderson Silva - 10 months ago 52
Python Question

How to manage different labels in a bar chart, taking data from a text file?

I'm new in using matplotlib, so I'm having some problems. I must create a bar chart with different labels, for each website that I have.
The file is like the following:

1001 adblock 12
1001 badger 11
1001 disconnect 15
1001 ghostery 15
1001 nottrack 14
1001 origin 15
1001 policy 16
1001 ultimate 14
4ruote adblock 12
4ruote badger 1
4ruote disconnect 14
4ruote ghostery 27
4ruote nottrack 9
4ruote origin 26
4ruote policy 34
4ruote ultimate 20
...... ........ ...

My goal is to create a bar chart in which I have:

  1. on the x axis sites (first column of the file), is a string

  2. on the y axis the values (third column of the file) for that site (that are 8 times repeated inside the file), so 8 integer values

  3. labels that, for a specific site, are present in the second column (strings).

I read different answers but each one didn't threat this comparison between labels, for a same variable.
What I'm doing is read the file, splitting the row and taking the first and third column, but how can I manage the labels?

Answer Source

seaborn does this neatly:

from pandas import read_csv
from matplotlib.pyplot import show
from seaborn import factorplot

fil = read_csv('multi_bar.txt', sep=r'\s*', engine='python', header=None)

factorplot(data=fil, x='site', y='value', hue='type', kind='bar')


enter image description here