2019, Александр Дьяконов www.dyakonov.org/ag
# подгружаем все нужные пакеты
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# для встроенных картинок
%pylab inline
# чуть покрасивше картинки:
############pd.set_option('display.mpl_style', 'default')
import warnings
warnings.filterwarnings("ignore")
#plt.rcParams['figure.figsize'] = 10, 7.5
#plt.rcParams['axes.grid'] = True
pd.set_option('display.max_columns', None)
plt.rcParams["font.family"] = "verdana"
Populating the interactive namespace from numpy and matplotlib
# переменные
x = [1, 2, 3, 5, 8, 9]
y = [3, 3, 2, 4, 1, 1]
# plt.figure(figsize=(7, 3)) # можно не писать
plt.plot(x, y)
[<matplotlib.lines.Line2D at 0x99062b0>]
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(7, 3)) # подготовка зоны для рисования
plt.plot(x, y, c='green', lw=2) # отрисовка
plt.text(3.3, 0, r'$\sin(x)$', fontsize=15, rotation=-60) # изображение текста
plt.xlabel('ось $x$') # подпись оси x
plt.ylabel('ось $y$') # подпись оси y
plt.title('график', family="verdana") # заглавие
plt.xticks(np.linspace(0, 10, 11)); # сетка по x
plt.yticks(np.linspace(-1, 1, 5)); # сетка по y
plt.xlim([0, 10]) # лимиты по оси x
plt.ylim([-1, 1]) # лимиты по оси y
plt.grid()
fig = plt.figure(figsize=(7, 3))
for t, i in enumerate(np.linspace(0, 1, 10)):
plt.plot(x+3*i, y+i, c=(i, i, 1-i), lw=2, label='график '+str(t))
plt.legend(loc=(1.01, 0), frameon=True, ncol=2,
fancybox=True, framealpha=1, shadow=True, borderpad=1)
plt.savefig('pic_subs.png', format='png', bbox_inches='tight')
plt.savefig('pic_subs.pdf', format='pdf', bbox_inches='tight')
plt.savefig('pic_subs.ps', format='ps', bbox_inches='tight')
plt.savefig('pic_subs.eps', format='eps', bbox_inches='tight')
plt.savefig('pic_subs.svg', format='svg', bbox_inches='tight')
plt.figure()
# первый график
plt.subplot(2, 1, 1) # (rows, columns, panel number)
plt.plot(x, np.sin(x), '-', lw=1)
# второй график
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x), '--', lw=2);
fig, ax = plt.subplots(2)
# первый график
ax[0].plot(x, np.sin(x), '-.', lw=3)
# второй график
ax[1].plot(x, np.cos(x), ':', lw=4);
# переменные
x = [1, 2, 3, 5, 8, 9]
y = [3, 3, 2, 4, 1, 1]
plt.figure(figsize=(3, 3))
plt.scatter(x, y)
<matplotlib.collections.PathCollection at 0x9e95080>
#rng = np.random.RandomState(0)
n = 15
x = rng.randn(n)
y = rng.randn(n)
colors = np.random.rand(n)
sizes = 1000 * np.random.rand(n)
plt.figure(figsize=(3, 3))
plt.scatter(x, y, c=colors, s=sizes,
alpha=0.3,
cmap='viridis')
plt.colorbar();
x = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([3, 1, 3, 2, 2, 4, 0])
z = np.array([1, 2, 1, 1, 2, 2, 3])
plt.figure(figsize=(7, 3))
plt.errorbar(x, y, yerr=z,
fmt='o', color='black',
ecolor='lightgray', elinewidth=5,
capsize=10);
plt.xlim([0, 8])
plt.ylim([-4, 7])
(-4, 7)
plt.figure(figsize=(7, 3))
plt.plot(x, y, '-', lw=2, color='blue')
plt.fill_between(x, y-z, y+z, color='blue', alpha=0.2)
plt.xlim([0, 8])
plt.ylim([-4, 7])
(-4, 7)
plt.figure(figsize=(7, 3))
plt.bar(x, y, color='blue', alpha=0.5)
<Container object of 7 artists>
x1 = np.random.normal(0, 0.8, 100)
x2 = np.random.normal(-2, 1, 100)
#kwargs = dict(bins=20, histtype='stepfilled', alpha=0.5, normed=True)
plt.figure(figsize=(7, 3))
plt.hist(x1, bins=20, alpha=0.5, color='blue');
plt.hist(x2, bins=20, alpha=0.5, color='green');
plt.scatter(x1, 12*np.random.rand(100) - 13, c='blue', alpha=0.5)
plt.scatter(x2, 12*np.random.rand(100) - 13, c='green', alpha=0.5)
<matplotlib.collections.PathCollection at 0xfd865f8>
x = np.random.randn(1000)
y = x + np.random.randn(1000)
plt.figure(figsize=(3, 3))
plt.scatter(x, y, 20, 'blue', alpha=0.5, edgecolor='white', linewidth=0.1)
<matplotlib.collections.PathCollection at 0xfdfa048>
plt.figure(figsize=(3, 3))
plt.hist2d(x, y, bins=20, cmap='Blues');
import seaborn as sns
# sns.set(rc={'figure.figsize':(3, 3)})
sns.jointplot(x, y, kind='kde');
import seaborn as sns
# sns.set(rc={'figure.figsize':(3, 3)})
sns.jointplot(x, y, kind='scatter');
fracs = [30, 15, 45, 10]
colors = ['#000055', '#222288', '#4444BB', '#6666FF']
fig, ax = plt.subplots(figsize=(5, 5))
pie = ax.pie(fracs, colors=colors, explode=(0.02, 0.02, 0.02, 0.15), shadow=True,
labels=['A', 'B', 'C', 'D'])