In [62]:
%config InlineBackend.figure_formats
Out[62]:
In [63]:
%config InlineBackend.figure_formats = ['svg']
In [64]:
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)
In [65]:
plt.plot([1,2,3,4])
plt.show()
In [66]:
plt.plot([1,2,3,4], [1,4,9,16])
plt.show()
plot(xy, color)
In [67]:
plt.plot([1,2,3,4], [1,4,9,16], 'r')
plt.show()
In [68]:
mpl.colors.cnames
Out[68]:
In [69]:
plt.plot([1,2,3,4], [1,4,9,16], ':')
plt.show()
In [70]:
plt.plot([1,2,3,4], [1,4,9,16], 'r--')
plt.show()
plot(xy, color-linestyle-marker)
In [71]:
plt.plot([1,2,3,4], [1,4,9,16], 'o')
plt.show()
In [72]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.show()
In [73]:
plt.plot([1,2,3,4], [1,4,9,16], color="magenta", linestyle="--", markeredgecolor='r', markerfacecolor="orange", marker="^", markersize=20)
plt.show()
In [74]:
plt.plot([1,2,3,4], [1,4,9,16], 'r:o')
plt.show()
axis
axis([xmin, xmax, ymin, ymax])
axis('off')
: turns off the axis lines and labels.:axis('equal')
: changes limits of x or y axis so that equal increments of x and y have the same length; a circle is circular.:axis('scaled')
: achieves the same result by changing the dimensions of the plot box instead of the axis data limits.:axis('tight')
: changes x and y axis limits such that all data is shown.
In [75]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis([0, 5, 0, 18])
plt.show()
In [76]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('off')
plt.show()
In [77]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('equal')
plt.show()
In [78]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('scaled')
plt.show()
In [79]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('tight')
plt.show()
In [80]:
t = np.arange(0., 5., 0.2)
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
In [81]:
plt.figure(figsize=(8, 6))
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-")
plt.plot(X, S, color="green", linewidth=1.0, linestyle="-")
plt.xlim(-4.0, 4.0)
plt.ylim(-1.0, 1.0)
plt.xticks(np.linspace(-4, 4, 9))
plt.yticks(np.linspace(-1, 1, 5))
plt.show()
In [82]:
# %matplotlib qt
f1 = plt.figure(1, figsize=(2, 2))
plt.plot([1,2,3,4], [1,4,9,16], '-o')
f2 = plt.figure(2, figsize=(6, 6))
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.show()
In [83]:
plt.figure()
plt.plot(X, C, color="blue", linewidth=2, linestyle="-")
plt.plot(X, S, color="red", linewidth=5, linestyle="--")
Out[83]:
In [84]:
plt.figure()
plt.plot(X, C)
plt.xlim(X.min() * 1.1, X.max() * 1.1);
plt.ylim(C.min() * 1.1, C.max() * 1.1);
In [85]:
plt.figure()
plt.plot(X, C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi]);
plt.yticks([-1, 0, +1]);
In [86]:
plt.figure()
plt.plot(X, C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']);
plt.yticks([-1, 0, +1], [r'$-1$', r'$0$', r'$+1$']);
In [87]:
plt.plot(X, C, label="cosine")
plt.plot(X, S, label="sine")
plt.legend(loc='upper left')
Out[87]:
In [88]:
plt.plot(X, C, label="cosine")
t = 2 * np.pi / 3
plt.scatter(t, np.cos(t), 50, color='blue')
plt.annotate(r'$cos(\frac{2\pi}{3})=-\frac{1}{2}$', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50),
textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->"))
Out[88]:
In [89]:
plt.plot(X, C)
plt.grid(False)
In [90]:
with plt.xkcd():
plt.title('XKCD style plot!!!')
plt.plot(X, C, label="cosine")
t = 2 * np.pi / 3
plt.scatter(t, np.cos(t), 50, color='blue')
plt.annotate(r'0.5 Here!', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50),
textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->"))
Children
Methods
cla()
In [91]:
X = np.random.normal(0,1,1024)
Y = np.random.normal(0,1,1024)
plt.scatter(X,Y)
Out[91]:
In [92]:
with plt.xkcd():
plt.figure(figsize=(5, 5))
Z = np.random.uniform(0, 1, 20)
plt.pie(Z, colors=mpl.cm.Set2(np.arange(20)/20.));
plt.axis('equal');
In [93]:
n = 12
X = np.arange(n)
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
for x, y in zip(X, Y1):
plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
for x, y in zip(X, -Y2):
plt.text(x + 0.4, y - 0.05, '%.2f' % y, ha='center', va='top')
plt.xlim(-0.25, 12)
plt.ylim(-1.25, +1.25)
Out[93]:
In [94]:
x = np.random.randn(5000)
arrays, bins, patches = plt.hist(x, bins=50, normed=True)
In [95]:
arrays
Out[95]:
In [96]:
bins
Out[96]:
In [97]:
patches
Out[97]:
In [98]:
def f(x, y):
return (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 -y ** 2)
n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
XX, YY = np.meshgrid(x, y)
ZZ = f(XX, YY)
plt.contourf(XX, YY, ZZ, alpha=.75, cmap='jet')
plt.contour(XX, YY, ZZ, colors='black', linewidth=.5)
Out[98]:
In [99]:
plt.imshow(ZZ, interpolation='nearest')
plt.grid(False)
In [100]:
plt.imshow(ZZ)
plt.grid(False)
In [101]:
# %matplotlib qt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
XX, YY = np.meshgrid(X, Y)
RR = np.sqrt(XX**2 + YY**2)
ZZ = np.sin(RR)
ax.plot_surface(XX, YY, ZZ, rstride=1, cstride=1, cmap='hot')
Out[101]:
In [108]:
mpl.cm.cmap_d
Out[108]:
In [109]:
cm1 = plt.get_cmap('gray')
print(cm1(0))
print(cm1(0.5))
print(cm1(1))
In [110]:
cm2 = mpl.colors.ListedColormap(['red', 'green', 'blue'])
print(cm2(np.linspace(0, 1, 10)))
In [111]:
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
plt.subplot(311);
plt.imshow(gradient, aspect='auto', cmap=plt.get_cmap('Set2'))
plt.grid(False)
plt.subplot(312);
plt.imshow(gradient, aspect='auto', cmap=cm1)
plt.grid(False)
plt.subplot(313);
plt.imshow(gradient, aspect='auto', cmap=cm2)
plt.grid(False)
In [112]:
d = np.random.normal(size=100)
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot(d, kde=False, color="b", ax=axes[0, 0])
sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])
sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])
sns.distplot(d, color="m", ax=axes[1, 1])
Out[112]:
In [113]:
df = sns.load_dataset("iris")
sns.pairplot(df, hue="species")
Out[113]:
In [114]:
tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")
sns.despine(offset=10, trim=True)
In [121]:
from bokeh.plotting import figure, output_notebook, show
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
output_notebook()
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
p.line(x, y, legend="Temp.", line_width=2)
show(p)
Out[121]: