Matplotlib条形图与不同的颜色条和条显示值

时间:2016-12-28 21:39:55

标签: python python-2.7 matplotlib bar-chart data-visualization

我想更改此代码中条形的颜色,所有条形图都是相同的颜色,我想在每个条形图的顶部显示不同的值,这些值存储在maxtweet,p,n变量中。

x=[]
x.append(max_tweets)
x.append(p)
x.append(n)
label=('tweets','positivity','nagitivity')
label_pos=np.arange(len(label))
plt.bar(label_pos,x,align='center',color='k')
plt.xticks(label_pos,label)
plt.xlabel('People Behaviour and Emotions')
plt.title('Sentiment Analysis')
plt.show()

1 个答案:

答案 0 :(得分:2)

from textblob import TextBlob;
from textblob.classifiers import NaiveBayesClassifier;
import sys;

def machineLearning(issue):
    train = [
        ('I cannot enter or exit the building','idCard'),
        ('Enter or exit', 'idCard'),
        ('Card needs replacing','idCard'),
        ('ID card not working','idCard'),
        ('Printing', 'printer'),
        ("My ID Card doesn't sign me into the printers", "printer"),
        ('Mono', "printer"),
        ('Colour Printing',"printer"),
        ('I cannot connect my phone to the wifi', 'byod'),
        ('I do not get emails on my phone, tablet, or laptop', 'byod'),
        ('Cannot send emails','email'),
        ('Do not recieve emails','email'),
        ('Cannot sign in to moodle', 'email'),
        ('Cannot sign in to computer','desktop_pc'),
        ("Software isn't working",'desktop_pc'),
        ('I cannot find my work', 'lost_work'),
        ('My work has been corrupted', 'lost_work'),
        ('My work has been lost', 'lost_work'),
        ('Coursework gone missing', 'lost_work')
    ];

    test = [
        ("My id card does not allow me to go through the gates, the light flashes orange and then goes red. The gate doesn't open", "idCard"),
        ('My id card has snapped', 'idCard'),
        ('When printing, swiping my ID card does not automatically sign me in, I have to manually register my card', 'printer'),
        ('I am getting errors when trying to sign into my email account on the website', 'email'),
        ('Microsoft applications were not working','desktop_pc'),
        ('Software is missing','desktop_pc'),
        ('software keeps crashing','desktop_pc'),
        ('my phone refuses to connect to the internet','byod'),
        ('I cannot sign in to the computers, moodle or emails because I have forgotten my password','desktop_pc'),
        ('I cannot get my emails on my device I bought from home','byod'),
        ('My ID card does not allow me to print. I go to scan my card on the printer, and it comes up with a message saying that I need to associate my card every day I want to print. Other peoples cards work fine?','idCard'),
        ('My password does not allow me to sign in to the computers, laptops, moodle or emails','forgotten_password'),
        ('I have forgotten my password','forgotten_poassword'),
    ];

    cl = NaiveBayesClassifier(train);

    print(cl.classify(issue));

if __name__ == "__main__": 
    machineLearning(sys.argv[1]);

输出:

enter image description here