Today we will see how to create simple plot graphs in Python using Seaborn. I have found in a Data Science book (by Sinan Ozdemir) a simple graph where we can plot sales and expenditure in advertising for different media like TV, Radio, Newspaper.
Let’see how it works. Where is better to put the money? First of all import pandas and seaborn packages and dataset
import pandas as pd
import seaborn as sns
data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0) #import data in CSV format using Pandas
data.head() # let'see how data are structured
TV radio newspaper sales
1 230.1 37.8 69.2 22.1
2 44.5 39.3 45.1 10.4
3 17.2 45.9 69.3 9.3
4 151.5 41.3 58.5 18.5
5 180.8 10.8 58.4 12.9
Let’s plot the data using some magic… and using seaborne package we can say to Python, to plot data having 3 x_vars, based on our 3 first column of our database, and on y_vars sales. Let’s see the result
sns.pairplot(data, x_vars=['TV','radio','newspaper'], y_vars=['sales'], size=4.5, aspect=0.7)
But image to write the same commands now with Radio & Newspaper with capital letter. Python is very sensitive (or at least Anaconda version that I’m using), so take care of using correct name of variable.
sns.pairplot(data, x_vars=['TV','Radio','Newspaper'], y_vars=['sales'], size=4.5, aspect=0.7)
Where to find correct Variable name? You can see it from:
- the head of your table
- In Anaconda, on the Variable explorer in column Value
If you need further info on magic function, %matplotlib inline, you can have a look to this post on Stackoverflow
Did you like this post on a simple plot graph. Share it to friends, colleagues or martians using below buttons and do not forget to subscribe to our fun newsletter.