Matplotlib is basic plotting library for Python inspired by Matlab. Seaborn is built on top of it with integrated analysis and specialized plots + pretty good integration with Pandas
Also see the full gallery of Seaborn or Matplotlib.
In [2]:
#disable some annoying warning
import warnings
warnings.filterwarnings('ignore', category=FutureWarning)
#plots the figures in place instead of a new window
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
In [3]:
#use a standard dataset of heterogenous data
cars = pd.read_csv('data/mtcars.csv')
cars.head()
Out[3]:
In [4]:
plt.scatter(x=cars['mpg'],y=cars['wt'])
plt.xlabel('miles per gallon')
plt.ylabel('weight')
plt.title('MPG vs WT')
plt.show()
In [5]:
#integrated in pandas, too
cars.plot(x='mpg',y='wt',kind='scatter')
Out[5]:
In [6]:
cars.plot(kind='scatter', x='mpg',y='wt',c='hp',s=cars['cyl']*20,alpha=0.5)
Out[6]:
In [7]:
#what if we plot everything?
cars.plot()
Out[7]:
In [8]:
cars['mpg'].hist(bins=5)
Out[8]:
In [9]:
plt.hist(cars['mpg'],bins=5)
plt.title('miles per gallon')
Out[9]:
In [10]:
#seaborn not just a histogram but also an kernel density enstimation and better default settings
sns.distplot(cars['mpg'],bins=5)
Out[10]:
In [11]:
#box plots
cars['mpg'].plot(kind='box')
Out[11]:
In [12]:
cars.boxplot('mpg')
Out[12]:
In [13]:
#group by gear
cars.boxplot('mpg', by='gear')
Out[13]:
In [14]:
# load gapminder again and select 2007
gap = pd.read_csv('data/gapminder-unfiltered.tsv',index_col=0, sep='\t')
gap2007 = gap[gap.year == 2007]
gap2007.columns
Out[14]:
In [15]:
gap2007.plot(kind='scatter', x='lifeExp',y='gdpPercap')
Out[15]:
unbalanced with outliers what about log scale?
In [16]:
gap2007.plot(kind='scatter', x='lifeExp',y='gdpPercap')
plt.yscale('log')
In [17]:
#create a color palette
colors = sns.color_palette()
sns.palplot(colors)
In [18]:
#for each group create an own plot an overlay them
for (name, group),color in zip(gap2007.groupby('continent'),colors):
plt.scatter(x=group['lifeExp'],y=group['gdpPercap'],label=name, c=color,s=30)
plt.yscale('log')
plt.legend()
Out[18]:
In [19]:
#playing with categories ... seaborn is pretty good with it
plt.figure(figsize=(40,20))
plt.subplot(121)
sns.boxplot(x='continent',y='gdpPercap',data=gap)
plt.subplot(122)
sns.violinplot(x='continent',y='gdpPercap',data=gap2007)
Out[19]:
In [20]:
# or with linear regression
anscombe = sns.load_dataset("anscombe")
sns.lmplot('x','y',col='dataset',hue='dataset', data=anscombe, col_wrap=2)
#g = sns.FacetGrid(anscombe, col="dataset", size=4, aspect=1)
#g.map(sns.regplot, "x", "y")
Out[20]:
In [21]:
# or with structured heatmaps
#compute the correlations and take a look at them
corrmat = gap.corr()
# draw a clustered heatmap using seaborn
sns.clustermap(corrmat, square=True)
Out[21]:
In [ ]:
#your code
In [21]:
from IPython.html.widgets import interact, interact_manual
In [22]:
@interact(text='Hello', slider=(0,10),check=True,categories=['red','green','blue'])
def react(text, slider,check,categories):
print(text,slider*10,check,categories)
In [23]:
@interact_manual(text='Hello', slider=(0,10),check=True,categories=['red','green','blue'])
def react(text, slider,check,categories):
print(text,slider*10,check,categories)
In [24]:
@interact(bins=(5, 25, 5),color=['red','green','orange','blue'])
def show_distplot(bins,color):
cars['mpg'].hist(bins=bins, color=color)
In [25]:
#hard core
from IPython.html import widgets
[widget for widget in dir(widgets) if not widget.endswith('Widget') and widget[0] == widget[0].upper() and widget[0] != '_']
Out[25]:
In [26]:
@interact(bins=widgets.FloatTextWidget(value=5))
def show_distplot(bins):
cars['mpg'].hist(bins=bins)
In [27]:
text_widget = widgets.Textarea(value='Hello', description='text area')
slider_widget = widgets.BoundedFloatText(5,min=0,max=10, description='slider area')
check_widget = widgets.Checkbox(True,description="CheckboxWidget")
toggle = widgets.RadioButtons(options=['red','green','blue'], description="RadioButtonsWidget")
@interact(text=text_widget, slider=slider_widget,check=check_widget,categories=toggle)
def react(text, slider,check,categories):
print(text,slider*10,check,categories)
In [28]:
b = widgets.Button(description="Update")
checkbox = widgets.Checkbox(description="CheckboxWidget")
tab1_children = [b,
checkbox,
widgets.Dropdown(options=['A','B'], description="DropdownWidget"),
widgets.RadioButtons(options=['A','B'], description="RadioButtonsWidget"),
widgets.Select(options=['A','B'], description="SelectWidget"),
widgets.Text(description="TextWidget"),
widgets.Textarea(description="TextareaWidget"),
widgets.ToggleButton(description="ToggleButtonWidget"),
widgets.ToggleButtons(options=["Value 1", "Value2"], description="ToggleButtonsWidget"),
]
tab2_children = [widgets.BoundedFloatText(description="BoundedFloatTextWidget"),
widgets.BoundedIntText(description="BoundedIntTextWidget"),
widgets.FloatSlider(description="FloatSliderWidget"),
widgets.FloatText(description="FloatTextWidget"),
widgets.IntSlider(description="IntSliderWidget"),
widgets.IntText(description="IntTextWidget"),
]
tab1 = widgets.Box(children=tab1_children)
tab2 = widgets.Box(children=tab2_children)
i = widgets.Accordion(children=[tab1, tab2])
i.set_title(0,"Basic Widgets")
i.set_title(1,"Numbers Input")
from IPython.display import display
def button_clicked(bb):
print(checkbox.value)
#TODO update plot
b.on_click(button_clicked)
display(i)
In [29]:
#your code