Home » Python1 February 2016

Python: Graph plotting with Matplotlib (Line Graph)

This article deals with plotting line graphs with Matplotlib (a Python’s library).

In the article Machine Learning & Sentiment Analysis: Text Classification using Python & NLTK, I had described about evaluating three different classifiers’ accuracy using different feature sets. In this article, I will be using the accuracy result data obtained from that evaluation.

Below is the result data which we will use to plot line graph. The data is saved in a CSV file named result4-accuracy-blog.csv.

classifierf1-all-wordsf1-all-words-stopwordsf1-bigramsf1-bigrams-stopwords
nb-cv0.74330890870.82347607750.83936915610.8701525025
maxent-cv0.72531488380.80717900280.81746868970.8649529103
svm-cv0.87368060020.88224188340.87975462370.8942136239

As seen above, the CSV file contains accuracy result in terms of F1 measure while using cross-validation during evaluation.

Row 1 = Heading information
Row 2 = Accuracy result for Naive Bayes Classifier
Row 3 = Accuracy result for Maximum Entropy Classifier
Row 4 = Accuracy result for Support Vector Machine Classifier
Col 1 = Accuracy result using “all words” feature set
Col 2 = Accuracy result using “all words with stopword filter” feature set
Col 3 = Accuracy result using “bigram words” feature set
Col 4 = Accuracy result using “bigram words with stopword filter” feature set

Here is the full Python & Matplotlib code to plot the line graph:

Here is the image of graph drawn:

line graph matplotlib

Drawing two plots in a single figure

Below is the data from an experiment done for one of my projects on Recommender System. I have used Mean Absolute Error (MAE) as the evaluation metrics. 5-fold cross validation was done using different ratings (user ratings, review ratings, combined ratings). The 5 rows represent MAE value from the 5-fold cross validation result. The data is saved in a CSV file named result3-blog2.csv.

user-rating-maereview-rating-maecombined-rating-maereview-rating-mae-mlcombined-rating-mae-ml
0.76005473530.79023724190.64653611660.78369693620.7170221067
0.82378628340.78567033760.6723066410.76442750490.7477524197
0.79758648510.89033823380.72906562840.82047932970.692127153
0.95264987660.79191942160.83697514160.79316554630.8319407532
0.69294690330.862918940.61987575850.64312044880.6824413178

Here is the full Python & Matplotlib code to plot the line graph (2 line graphs in a single figure):

Here is the image of graph drawn:

line graph matplotlib

Hope this helps.
Thanks.

Python

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed