matplotlib: グラフ描画 https://matplotlib.org/stable/index.html
mplot3d: 3d描画 https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
seaborn: matplotlibをきれいに見せるラッパー https://seaborn.pydata.org/
matplotlib tutorial: https://matplotlib.org/stable/tutorials/index.html
import numpy as np
import matplotlib.pyplot as plt
def get_1d(reso=20):
x_range = (0, 10)
xs = np.linspace(x_range[0], x_range[1], reso)
ys = np.sin(xs)
return xs, ys, x_range
xs, ys, x_range = get_1d()
plt.plot(xs, ys)
plt.show()
xs, ys, x_range = get_1d()
fig = plt.figure()
ax = fig.add_subplot(111) # 3d plot の時には、add_subplot(111, projection='3d')
ax.plot(xs, ys)
plt.show()
xs, ys, x_range = get_1d()
fig = plt.figure(figsize=(10, 5))
fig.subplots_adjust(wspace=0.3, hspace=0.4)
for i in range(6):
ax = fig.add_subplot(2, 3, i + 1)
ax.plot(xs, ys)
plt.show()
xs, ys, x_range = get_1d()
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot(111)
ax.plot(
xs, ys,
marker='',
color='#2f2',
linestyle='-',
linewidth=3,
label='fitting',
)
ax.plot(
xs, ys,
marker='o',
markersize='10',
markerfacecolor='#090',
markeredgecolor='k',
linestyle='none',
label='data',
)
ax.grid(True)
ax.legend(loc='upper center') # upper left, lower right, upper center ...
plt.show()
https://matplotlib.org/stable/index.html
matplotlibでグラフの文字サイズを大きくする https://qiita.com/Tatejimaru137/items/4ee6a73114d07d85bfd7
xs, ys, x_range = get_1d()
ys = ys + 1
fig = plt.figure(figsize=(6,4))
plt.rcParams['font.size'] = 18 # これを実行すると全てに影響を与えるので最後にもどす
ax = fig.add_subplot(111)
ax.plot(xs, ys, label='data')
ax.set_ylim(-0.2, 2.2)
ax.set_yticks([0, 1, 2])
ax.grid(True)
ax.set_title('sin')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
plt.tight_layout()
# plt.savefig('figure.png') # 画像を保存するとき
plt.show()
plt.rcParams['font.size'] = 12 # 戻す
xs, ys, x_range = get_1d()
ys = ys + 3
ys_up = ys + 1
ys_low = ys -1
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot(111)
ax.fill_between(
xs, ys_up, ys_low,
alpha=0.1,
color='k',
edgecolor='none',
)
ax.plot(xs, ys, 'k', lw=3)
ax.grid(True)
plt.show()
ヒストグラムを計算する場合には、histを使えば自動で棒グラフが描画できる https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.hist.html?highlight=hist#matplotlib.axes.Axes.hist
xs, ys, x_range = get_1d(reso=5)
xs = np.arange(5)
ys += 3
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)
ax.bar(
xs, ys,
width=0.5, color='#aaa',
tick_label=['a', 'b', 'c', 'd', 'e'],
)
plt.show()
xs, ys, x_range = get_1d(reso=5)
xs = np.arange(5)
ys += 3
ys_err = ys * 0.2
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)
ax.bar(
xs, ys, yerr=ys_err,
width=0.5,
color='#aaa', edgecolor='#000', linewidth=1,
tick_label=['a', 'b', 'c', 'd', 'e'],
error_kw={'lw':1, 'capthick':1, 'capsize':5},
)
plt.show()
xs = np.arange(5)
ys1 = xs + 3
ys2 = ys1 + 1
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)
ax.bar(xs - 0.2, ys1, width=0.3, label='blue')
ax.bar(xs + 0.2, ys2, width=0.3, label='orange')
ax.set_xticks(xs)
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
plt.legend()
plt.show()