In [1]:
! pip install Seaborn
In [2]:
import numpy as np
from numpy.random import randn
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
from matplotlib import rcParams
import seaborn as sb
In [3]:
%matplotlib inline
rcParams['figure.figsize'] = 5,4
sb.set_style('whitegrid')
Plotting a line chart in matplotlib
In [5]:
x=range(1,10)
y=[1,2,3,4,0,4,3,2,1]
plt.plot(x,y)
Out[5]:
Plotting a line chart from a Pandas object
In [6]:
# address = some data set
# cars = pd.read_csv(address)
# cars.columns = ['car_names','mpg','cyl','disp','hp','drat','wt','qsec','vs','am',gear',carb']
#mpg = cars['mpg']
#mpg.plot()
Creating bar charts:
In [7]:
plt.bar(x,y)
Out[7]:
In [8]:
#Creating bar chart from pandas object
#mpg.plot(kind='bar')
Creating a pie chart
In [9]:
x=[1,2,3,4,0.5]
plt.pie(x)
plt.show()
In [10]:
#plt.savefig('pie_chart.jpeg') Saves plot as jpeg in wd
#plt.show()
Defining axes, ticks, and grids
In [11]:
x=range(1,10)
y=[1,2,3,4,0,4,3,2,1]
fig=plt.figure()
ax = fig.add_axes([.1, .1,1,1])
ax.plot(x,y)
Out[11]:
In [12]:
fig = plt.figure()
ax = fig.add_axes([.1, .1,1,1])
ax.set_xlim([1,9])
ax.set_ylim([0,5])
ax.set_xticks([0,1,2,4,5,6,8,9,10])
ax.set_yticks([0,1,2,3,4,5])
ax.plot(x,y)
Out[12]:
In [14]:
fig = plt.figure()
ax = fig.add_axes([.1, .1,1,1])
ax.set_xlim([1,9])
ax.set_ylim([0,5])
ax.grid() #seems like this takes away grid? different than tutorial
ax.plot(x,y)
Out[14]:
In [15]:
fig = plt.figure()
fig, (ax1,ax2)=plt.subplots(1,2)
ax1.plot(x)
ax2.plot(x,y)
Out[15]:
In [16]:
x = range(1,10)
y= [1,2,3,4,0.5,4,3,2,1]
plt.bar(x,y)
Out[16]:
In [17]:
wide=[0.5,0.5,0.5,0.9,0.9,0.5,0.5,0.9,0.9]
color = ['salmon']
plt.bar(x, y, width=wide, color=color, align='center')
Out[17]:
Note: with pandas objects
color_theme = ['darkgray','lightsalmon','powderblue'] df.plot(color=color_theme)
In [21]:
z = [1,2,3,4,0.5]
color_theme = ['#A9A9A9', '#FFA07A', '#B0E0E6','#FFE4CA','#BDB76B'] #hex codes
plt.pie(z, colors = color_theme)
plt.show()
In [22]:
#line styles
x1= range(0,10)
y1=[10,9,8,7,6,5,4,3,2,1]
plt.plot(x,y,ls = 'steps', lw = 5)
plt.plot(x1,y1, ls = '--', lw = 10)
Out[22]:
In [23]:
#plot markers
plt.plot(x,y,marker = '1', mew=20)
plt.plot(x1,y1, marker = '+', mew=15)
Out[23]:
In [24]:
#functional method
x= range(1,10)
y=[1,2,3,4,0.5,4,3,2,1]
plt.bar(x,y)
plt.xlabel('your x-axis label')
plt.ylabel('your y-axis label')
Out[24]:
In [25]:
z= [1,2,3,4,0.5]
veh_type = ['bicycle', 'motorbike','car','van','stroller']
plt.pie(z, labels= veh_type)
plt.show()
In [26]:
#object oriented has ways of doing this too
#uses cars dataset
#didn't copy down everything
#fig = plt.figure()
#ax = fig.add_axes([])
#mpg.plot()
#ax.set_xticks(range(32))
#ax.set_xticklabels()
#ax.set_title('Title goes here')
#ax.set_xlabel('car names')
#ax.set_ylabel('miles/gal')
#ax.legend(loc='best')
In [27]:
#add legend
plt.pie(z)
plt.legend(veh_type, loc='best')
plt.show()
In [28]:
#annotate
#object oriented: ax.annotate('Toyota Corolla', xy=(19,33,9), xytext=(21,35),
#arrowprops = dict(facecolor='black',shrink=0.05))
In [29]:
#address = 'address'
#df = pd.read_csv(address, index_col='Order Date', parse_dates=True)
#df.head()
#df2 = df.sample(n=100, random_state=25, axis=0)
#plt.xlabel('Order Date')
#plt.ylabel('Order Quantity')
#plt.title('Superstore Sales')
#df2['Order Quantity'].plot()
In [30]:
from pandas.tools.plotting import scatter_matrix
In [31]:
#pandas dataset import
#mpg.plot(kind='hist')
# or plt.hist(mpg) with plt.show()
#with seaborn
#sb.distplot(mpg)
#scatterplots
#cars.plot(kind='scatter',x='hp',y='mpg',c=['darkgray'], s=150)
#sb.regplot(x='hp', y='mpg', data=cars, scatter=True)
#seaborn automatically creates trend line
#sb.pairplot(cars)
#get subset of data using dataframes:
#cars_df = pd.DataFrame((cars.ix[:,(1,3,4,6)].values), columns = ['mpg', 'disp','hp','wt'])
In [32]:
#boxplots
#cars.boxplot(column='mpg', by='am')
#in seaborn
#sb.boxplot(x='am', y='mpg', data=cars, palette='hls')
In [ ]: