====== matplotlib ======
===== Animation =====
* http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
* [[https://towardsdatascience.com/animate-your-graphs-in-python-in-4-easy-steps-243dccad9a7|Animate your Graphs in Python in 4 Easy Steps! (GIF 파일 만들기)]]
===== 한글 출력 =====
* 한글 폰트 설치
* 나눔 폰트 설치
* 폰트 캐시 삭제(폰트를 설치해도 폰트를 찾지 못할때)
* windows: ~/.matplotlib/fontList.cache 삭제
* ubuntu: ~/.cache/matplotlib/fontList.cache
import matplotlib
import matplotlib.font_manager
import matplotlib.pyplot as plt
# 'Nanum' 폰트 리스트 출력
print([f.name for f in matplotlib.font_manager.fontManager.ttflist if 'Nanum' in f.name])
# 폰트 변경
matplotlib.rc('font', family="NanumMyeongjo") # or "Noto Sans CJK KR", "Noto Sans Mono CJK KR"
plt.title("한글 제목")
plt.plot([1, 2, 3], [1, 2, 3])
===== 폰트 사이즈 변경 =====
import matplotlib.pyplot as plt
matplotlib.rcParams.update({'font.size': 22})
# or
plt.rcParams.update({'font.size': 22})
# tick label size
plt.rcParams.update({'xtick.labelsize': 22, 'ytick.labelsize': 22})
===== ticks format 변경 =====
- https://matplotlib.org/examples/pylab_examples/custom_ticker1.html
===== 예제 =====
plt.rcParams.update({'font.family': "NanumMyeongjo"})
# or "Noto Sans CJK KR", "Noto Sans Mono CJK KR"
plt.rcParams.update({'font.size': 22})
plt.rcParams.update({'xtick.labelsize': 22, 'ytick.labelsize': 22})
fig, ax = plt.subplots(figsize=(8, 4), clear=True)
ax.set_xlabel('Generation')
ax.set_ylabel(f'{args.tag}')
for cond in dfs:
line, = ax.plot(dfs[cond].index, dfs[cond]['50%'], '-', label=cond)
ax.fill_between(dfs[cond].index, dfs[cond]['25%'], dfs[cond]['75%'], color=line.get_color(), alpha=0.3)
ax.legend()
plt.show()
===== Seaborn =====
* https://towardsdatascience.com/everything-you-need-to-know-about-scatter-plots-for-data-visualisation-924144c0bc5
===== imshow =====
fig, (ax1, ax2) = plt.subplots(1, 2)
cs = list()
min_diff = 0.5
xs = np.arange(0, 10).reshape((-1, 1))
ax1.imshow(xs, cmap='gray')
for x in xs.reshape(-1):
b = x / 10
w = 1 - b
if b < 0.5:
c = (1 - min_diff) * w + min_diff
else:
c = (1 - min_diff) * w
c = np.clip(c, 0, 1)
cs.append(c)
ax1.text(0,
x,
f'{x}',
color=(c, c, c),
horizontalalignment='center',
verticalalignment='center')
ax2.scatter(xs, cs)
plt.show()
===== Radar chart =====
* https://matplotlib.org/3.1.1/gallery/specialty_plots/radar_chart.html#sphx-glr-gallery-specialty-plots-radar-chart-py
kvs = dict(a=1.0, b=0.2, c=0.4, d=0.5, e=0.7)
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
ax.set_title(f'Radar chart')
ax.set_ylim([0, 1.0])
theta = np.linspace(0.0, 2 * np.pi, len(kvs) + 1, endpoint=True)
ax.set_xticks(theta)
ax.set_xticklabels([f'{k}' for k in kvs])
vs = np.hstack([list(kvs.values()), [list(kvs.values())[0]]])
ax.plot(theta, vs)
ax.fill_between(theta, 0, vs)
plt.show()
plt.close()
===== Subplot =====
* https://matplotlib.org/3.1.1/gallery/recipes/create_subplots.html#sphx-glr-gallery-recipes-create-subplots-py
===== Embedded plot =====
fig, ax = plt.subplots(1, 1, figsize=(12.8, 9.6))
data = np.array([
np.random.random(2),
np.random.random(2),
np.random.random(2),
])
subdata = np.array([
np.random.random(3),
np.random.random(3),
np.random.random(3),
])
scatter = ax.scatter(data[:, 0], data[:, 1], cmap='jet')
ax.set_xlim([-0.2, 1.2])
ax.set_ylim([-0.2, 1.2])
for idx, (x, y) in enumerate(data):
subdata_ = subdata[idx]
box = ax.get_position()
width = box.width * 0.2
height = box.height * 0.2
xmin, xmax, ymin, ymax = ax.axis()
nx = (x - xmin) / (xmax - xmin) - width / 2
ny = (y - ymin) / (ymax - ymin) - height / 2
inax_position = ax.transAxes.transform([nx, ny])
transFigure = fig.transFigure.inverted()
infig_position = transFigure.transform(inax_position)
x = infig_position[0]
y = infig_position[1]
subax = fig.add_axes([x, y, width, height])
subax.get_xaxis().set_visible(False)
subax.get_yaxis().set_visible(False)
subax.plot(subdata_)
plt.show()
===== Neural Net Weights/Activation =====
* [[https://matplotlib.org/3.1.1/gallery/specialty_plots/hinton_demo.html#sphx-glr-gallery-specialty-plots-hinton-demo-py|hinton diagram]]