In [119]:
from IPython.core.display import HTML
print("Setting custom CSS for the IPython Notebook")
styles = open('custom.css', 'r').read()
#HTML(styles)
In [1]:
## all imports
import numpy as np
import pandas as pd
from pandas import Series
from pandas import DataFrame
pd.options.display.mpl_style = 'default'
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set_context("talk")
In [2]:
print "Hello CS109"
print "I love IPython"
In [123]:
url = 'https://raw.githubusercontent.com/cs109/2014/master/lectures/wur2013.txt'
ranking = pd.read_table(url)
ranking = ranking[:3000]
N = 50
colors = np.random.rand(N)
area = colors = np.random.rand(N)
plt.scatter(ranking.iloc[:,3], ranking.iloc[:,7])
plt.show()
In [129]:
matplotlib.pyplot.xkcd()
data_to_plot = ranking.overall
plt.bar(data_to_plot.index, data_to_plot)
plt.show()
In [128]:
data_to_plot.plot(kind='bar')
plt.show()
In [130]:
relevant_columns = ['academic','faculty','citations',
'employer','international']
ranking_categories=ranking[relevant_columns]
ranking_categories.head()
Out[130]:
In [131]:
weights = [0.4,0.2,0.2,0.1,0.1]
ranking_categories_weighted = ranking_categories * weights
ranking_categories_weighted.head()
Out[131]:
In [136]:
ranking_categories_weighted.head().plot(kind='bar')
plt.show()
In [11]:
ax = ranking_categories_weighted.head().plot(kind='bar', legend=False)
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(0, 0))
plt.show()
In [12]:
ranking_categories_weighted.head().plot(kind='bar', legend=False, stacked=True)
plt.show()
In [118]:
matplotlib.pyplot.xkcd()
ranking_categories_weighted.plot(kind='barh',
legend=False, stacked=True)
plt.show()
In [29]:
In [14]:
ranking.head().schoolname
ranking_categories.set_index(ranking.schoolname, inplace=True)
ranking_categories.plot(kind='barh', legend=False, stacked=True)
plt.show()
In [5]:
N = 50
colors = np.random.rand(N)
area = colors = np.random.rand(N)
ranking.iloc[:,3]
plt.scatter(ranking_categories_weighted.iloc[:,3], ranking_categories_weighted.iloc[:,3], s=1, c=colors, alpha=0.5)
plt.show()
In [14]:
In [14]:
In [14]:
In [15]:
titanic = sns.load_dataset("titanic")
titanic
titanic.age.hist(bins=20);
sns.lmplot("fare", "age", titanic, fit_reg=False);
sns.lmplot("fare", "age", hue="class", data=titanic, fit_reg=False)
sns.lmplot("fare", "age", hue="class", size="class", data=titanic, fit_reg=False)
sns.PairGrid(iris, vars=["sepal_length", "sepal_width"], hue="species")
fig = plt.figure()
ax = fig.add_subplot(1,1,1) # one row, one column, first plot
ax.set_title("At Bats vs. Hits. Size = Home Runs")
ax.set_xlabel("At Bats")
ax.set_ylabel("Hits")
sns.jointplot(titanic.fare,titanic.age);
plt.scatter(titanic.fare, titanic.age, s=titanic.pclass, alpha=0.5)
#xlim(0, 700); ylim(0, 200)
from mpl_toolkits.mplot3d import Axes3D
fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection='3d')
ax2.scatter(titanic.age, titanic.fare, titanic.pclass)
In [12]:
In [38]:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.image import BboxImage
from matplotlib._png import read_png
import matplotlib.colors
from matplotlib.cbook import get_sample_data
if 1:
from matplotlib.transforms import Bbox, TransformedBbox
from matplotlib.ticker import ScalarFormatter
fig = plt.gcf()
fig.clf()
ax = plt.subplot(111)
years = np.arange(2004, 2009)
# --- changed this line --- #
box_colors = sns.color_palette()
#brewer2mpl.get_map('Set1', 'qualitative', 5).mpl_colors
#heights = np.random.random(years.shape) * 10000 + 3000
heights = {100, 10, 100, 10, 10}
fmt = ScalarFormatter(useOffset=False)
ax.xaxis.set_major_formatter(fmt)
for year, h, bc in zip(years, heights, box_colors):
# --- this is the line we changed --- #
ax.bar(year-0.4, h, color=bc, linewidth=0)
ax.annotate(r"%d" % (int(h/100.)*100),
(year, h+100), va="bottom", ha="center")
ax.set_xlim(years[0]-0.5, years[-1]+0.5)
ax.set_yscale('log')
ax.set_ylim(1e1, 1e4)
# --- Added this line --- #
#ax.spines['top'].set_visible(False)
#ax.spines['right'].set_visible(False)
#ax.spines['left'].set_visible(False)
# --- Added this line --- #
ax.yaxis.set_ticks_position('none')
ax.xaxis.set_ticks_position('none')
#ax.grid(axis = 'y', color ='white', linestyle='-')
#fig.savefig('ribbon_box_no_ribbons.png')
plt.show()
In [29]:
import numpy as np
import pandas as pd
import seaborn as sns
x = 10**np.arange(1, 10)
y = 10**np.arange(1,10)*2
df1 = pd.DataFrame(data=y, index=x)
df2 = pd.DataFrame(data = {'x': x, 'y': y})
fgrid = sns.lmplot('x', 'y', df2)
ax = fgrid.axes[0][0]
df1.plot(ax=ax)
ax.set_xscale('log')
ax.set_yscale('log')
In [ ]: