강좌/MachineLearning
TensorFlow #005 파이썬 라이브러리로 그래프 그리기
여름나라겨울이야기
2016. 12. 6. 16:23
파이썬에서 그래프를 그리는 라이브러리 중에서 matplotlib 의 pyplot 를 살펴 보도록 하자.
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [1, 2, 3, 4, 5] plt.plot(xs, ys) plt.show() |

쿨럭, 나는 선이 아니라 점을 찍고 싶었을 뿐이고...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [1, 2, 3, 4, 5] plt.plot(xs, ys, 'o') plt.show() |

쿨럭, 나는 빨간 점을 찍고 싶었을 뿐이고...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [1, 2, 3, 4, 5] plt.plot(xs, ys, 'ro') plt.show() |
|

이쯤되면 라이브러리 API 를 살펴보고 싶을 뿐이고...
http://matplotlib.org/api/pyplot_api.html

쿨럭, 그런데 x 축, y 축을 0 ~ 6 까지 표기하고 싶을 뿐이고...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [1, 2, 3, 4, 5] plt.plot(xs, ys, 'ro') plt.xlim(0, 6) plt.ylim(0, 6) plt.show() |
|

쿨럭, x 축, y 축 둘다 0이 나와서 보기 싫을 뿐이고...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [1, 2, 3, 4, 5] plt.plot(xs, ys, 'ro') plt.xlim(0, 6) plt.ylim(0.1, 6) plt.show() |
|

(쿨럭, 더 나은 방법이 있지 않을까?)
쿨럭, 각 축에 라벨을 붙이고 싶었을 뿐이고...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [1, 2, 3, 4, 5] plt.plot(xs, ys, 'ro') plt.xlim(0, 6) plt.ylim(0.1, 6) plt.xlabel("x-axis") plt.ylabel("y-axis") plt.show() |
|

쿨럭.. 데이터에 라벨을 달고 싶을 뿐이고...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [1, 2, 3, 4, 5] plt.plot(xs, ys, 'ro', label='red point') plt.xlim(0, 6) plt.ylim(0.1, 6) plt.xlabel("x-axis") plt.ylabel("y-axis") plt.legend() plt.show() |
|

쿨럭.. 나는 단순 데이터가 아닌 함수식을 이용하고 싶을 뿐이고...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [] for x in xs: ys.append([x * x + 2 * x + 1]) plt.plot(xs, ys, 'ro', label='red point') plt.xlim(0, 6) plt.ylim(0.1, 30) plt.yticks(range(0, 40)) plt.xlabel("x") plt.ylabel("y") plt.legend(loc='lower right') plt.show() |

쿨럭... 이미지 사이즈가 좀...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [] for x in xs: ys.append([x * x + 2 * x + 1]) # width: 2 inches # height: 7 inches plt.figure(figsize=(2, 7)) plt.plot(xs, ys, 'ro', label='red point') plt.xlim(0, 6) plt.ylim(0.1, 30) plt.yticks(range(0, 40)) plt.xlabel("x") plt.ylabel("y") plt.legend(loc='lower right') plt.show() |

쿨럭.. 나는 2개 이상의 선을 그리고 싶고...
import matplotlib.pyplot as plt xs = [1, 2, 3, 4, 5] ys = [] ys2 = [] for x in xs: ys.append([x * x + 2 * x + 1]) ys2.append([5 * x + 1]) # width: 2 inches # height: 7 inches plt.figure(figsize=(4, 7)) plt.plot(xs, ys, 'r', label='red point') plt.plot(xs, ys2, 'b', label='blue point') plt.xlim(0, 6) plt.ylim(0.1, 30) plt.yticks(range(0, 40)) plt.xlabel("x") plt.ylabel("y") plt.legend(loc='lower right') plt.show() |
