티스토리 뷰
삽질의 시작¶
일단 공통으로 적용되는 import 파트는 다음과 같다. numpy
는 필수적인 것같다.
import numpy as np
import matplotlib.pyplot as plt
다음 순간 시작부터 태클에 걸렸다.
두 가지 plotting style 이 존재한단다!
Pyplot vs Object Oriented Interface(OO)¶
어떤 차이가 있는지 모르니 어느 쪽으로 시작해야할지 갈등이 있었다.
더우기 예제들이 양쪽을 번갈아 사용해니 답답하다. 두 방식을 알아나 보자.
pyplot
interface / functional interface.Object-Oriented interface (OO).
pyplot¶
MATLAB 의 method 를 흉내낸다고 한다. 흠, 어차피 난 MATLAB 을 모르잖아.
명시적으로 figure 와 axes 를 사용하지 않고 plotting 을 할거라는 건데, 뭔 말인지 잘 모르겠지만 code 입력이나 해보자.
X = np.arange(0., 10., 0.5)
Y = 2*X
plt.figure(figsize=(4,3), dpi=100)
plt.plot(X, Y,'bo-')
plt.xlabel("x-축")
plt.ylabel("y-축")
plt.legend(["흠..."])
plt.grid(True)
/Users/painless/opt/anaconda3/lib/python3.9/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 52629 (\N{HANGUL SYLLABLE CUG}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /Users/painless/opt/anaconda3/lib/python3.9/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 55136 (\N{HANGUL SYLLABLE HEUM}) missing from current font. fig.canvas.print_figure(bytes_io, **kw)
- 어라! 한글 출력이 안된다.
- plt.plot(...) 이 화면에 그려주는 것일텐데 다른 method 뒤따른다. 얘들은 어떻게 처리되지?
납득할 수 없는 것은 잠시 미뤄두고 알게 된 것부터 기록하자.
plt.plot(X, Y, 'bo-')
- 'bo-' 에서 'b' 는 color, 'o' 는 marker(지금은 circle) 그리고 '-' 는 선을 표시한단다.
- legend 에 '전설' 이 아닌 '그림의 설명' 이란 뜻이 있다는 것을 처음 알았다.
그런데 figure, axes 가 중요하다는데 그걸 알기 위해서라도 pyplot
대신 OO 를 공부하는게 낫겠다.
OO (Object-Oriented Interface)¶
같은 code 를 OO style 로 입력해본다.
앞서는 은폐되어있던 Figure, Axes
를
fig, ax
라는 변수로 드러내는 것 같은데...
fig, ax = plt.subplots(figsize=(4,3), dpi=100)
ax.plot(X, Y,'bo-')
# ax.xlabel 혹은 fig.xlabel 은 에러가 난다!
plt.xlabel("x-축")
plt.ylabel("y-축")
plt.legend(["흠..."])
plt.grid(True)
plt.show()
plot method 가 그림 그리기를 명령하는 줄 알았는데 아니었던 모양이다.
ax.plot(...)
는 아마도 그림에 필요한 것들이 다 준비됐어!
정도인가 보다.
그리고 정작 '그림을 그리는 행위'는 plt
가 담당하는 것같다.
아니면 plot 다음의 코드들은 설명이 잘 안된다.
(코멘트 해 두었지만 label, legend, grid 는 Axes
인 ax 가 그리지 않는다.)
물론, 다음과 같이 ax 에서 x 축의 이름을 설정할 수 있다.
ax.set_xlabel('x-축')
그런데, 앞에 접두어 'set_'이 붙는 것이 plt.xlabel(...)
과 차이가 있다.
뭐 정확히 어떻게 돌아가는 것인지 장담은 못하겠지만 대충 감 잡은 것 같기도...
아니다! Figure 와 Axes
는 어떻게 차이가 나는 거지?
Artist - Figure, Axes¶
공식 문서를 읽어보니 대충 눈에 보이는 모든 것은 추상 클래스 Artist 의 subclass 이고,
그 중 Figure는 최상위 container 역할,
Axes 는 data 가 처리되는 영역이라는데...
fig = plt.figure(figsize=(4,4), facecolor='blue')
ax = fig.add_subplot()
ax.plot([1,2,3,4,5], [1,2,3,2,1])
ax.patch.set_facecolor('#ffff00') ## axes 배경색
ax.tick_params(axis='x', colors='black')
ax.tick_params(axis='y', colors='white')
plt.show()
색을 집어넣고 보니 구별은 된다. 아! 머리 아프다.
그런데, 한글은?
에라, 다음에 처리하자.
- Total
- Today
- Yesterday
- lazy propagation
- 세그먼트 트리
- JavaScript
- script
- javascript array
- number theory
- BOJ
- stack
- math font
- max flow
- map
- Reference
- bash script
- fenwick tree
- Aho-Corasick
- C++ big number
- Shell Programming
- 정수론
- python3
- persistent segment tree
- 다익스트라
- Vim
- bash
- dynamic programming
- Dijkstra
- 백준
- RUBY
- segment tree
- nearest common ancestor
- shell
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |