GUEST0 pt
クイズ・バイオインフォの森
4. 【python】棒グラフ・折れ線グラフ・ヒストグラム・band【matplotlib】

4. 【python】棒グラフ・折れ線グラフ・ヒストグラム・band【matplotlib】

目次

1. 基本

subplotを使わない方法

subplotを使う方法

Text
# figure
plt.figure(figsize=(14, 6), dpi=100)
ax = plt.subplot(111)


# plot (ex. bar plot)
bars = []
xs = np.array(range(1, 50 + 1))
data = list(range(50))
plt.bar(xs, data, width=0.2, color='blue')


# text
x, y = 40, 15
plt.text(x, y, '*', fontsize=14)


# title
plt.title(f'BIC score per model')


# x axis config
plt.xticks(n_components_range)
ax.set_xlabel('Number of components')


# y axis config
plt.ylim([BICs.min() * 1.01 - .01 * BICs.max(), BICs.max()])


# legend
ax.legend('BIC')


# show
plt.show()

2. 棒グラフ

1系列の棒グラフ

Text
# figure
plt.figure(figsize=(14, 6), dpi=100)
ax = plt.subplot(111)


# plot (ex. bar plot)
bars = []
xs = np.array(range(1, 50 + 1))
data = list(range(50))
plt.bar(xs, data, width=0.2, color='blue')


# title
plt.title(f'BIC score per model')


# x axis config
plt.xticks(n_components_range)
ax.set_xlabel('Number of components')


# y axis config
plt.ylim([BICs.min() * 1.01 - .01 * BICs.max(), BICs.max()])


# legend
ax.legend('BIC')


# show
plt.show()

複数系列の棒グラフ

Text
import matplotlib.pyplot as plt


plt.figure(figsize=(14, 6),dpi=100)
ax = plt.subplot(111)


cv_colors = ['blue', 'green', 'red', 'orange']
cv_colors_iter = itertools.cycle(cv_colors)
n_components_range = range(1, 50 + 1)


# 棒グラフ
bars = []
for i, color in enumerate(cv_colors_iter):
	xposes = np.array(n_components_range) + .2 * (i - 2)
    L = len(n_components_range)
	bars.append(plt.bar(xposes, BICs[i*L:(i + 1)*L], width=.2, color=color))


# text
xpos = BICs.argmin() % len(n_components_range) + .65 + .2 * BICs.argmin() // len(n_components_range)
plt.text(xpos, BICs.min() * 0.97 + .03 * BICs.max(), '*', fontsize=14)


# layouts
plt.title(f'BIC score per model')
plt.xticks(n_components_range)
ax.set_xlabel('Number of components')
plt.ylim([BICs.min() * 1.01 - .01 * BICs.max(), BICs.max()])
ax.legend([b[0] for b in bars], cv_types)


# shoe
plt.show()

3. ヒストグラム

基本

Text
import matplotlib.pyplot as plt


pathway_dist = [len(ptm_syms[pt]) for pt in ptm_syms.keys()]
plt.hist(pathway_dist, bins=20)
plt.show()

y軸をlogスケールにする。

Text
import matplotlib.pyplot as plt


pathway_dist = [len(ptm_syms[pt]) for pt in ptm_syms.keys()]
plt.hist(pathway_dist, bins=20, log=True)
plt.show()

広告を取得中...
4. 【python】棒グラフ・折れ線グラフ・ヒストグラム・band【matplotlib】 | バイオインフォの森