파이썬 독학 day06 ---Matplotlib
14680 단어 파이썬 독학
일단 맏형이 쓴 마무리를 여기에 넣어보도록 하겠습니다.
대장부: 휴전 --> 파이톤--matplotlib 드로잉 시각화 지식 포인트 정리
Notzuonotdied --> Python Matplotlib 간단한 자습서
다음은 이 채소새가 연습한 노트..
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.gridspec as gridspec
from matplotlib import animation
#
# x = np.linspace(-1,1,50) # -1 1 50
# y = 2*x + 1
# plt.plot(x, y)
# plt.show()
#
#
# x = np.linspace(-3, 3, 50)
# y1 = 2*x + 1
# y2 = x**2
# plt.figure() # figure
# plt.plot(x, y1)
#
# plt.figure() # figure2
# plt.plot(x, y2)
#
# # figure
# plt.figure(num=3, figsize=(8, 5),) # figure 8,5
# plt.plot(x, y2)
# plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
# plt.show()
#
#
# x = np.linspace(-3, 3, 50)
# y1 = 2*x + 1
# y2 = x**2
#
# plt.figure()
# plt.plot(x, y2)
# plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
#
# plt.xlim((-1, 2)) # x
# plt.ylim((-2, 3)) # y
# plt.xlabel('I am x') # x lable x
# plt.ylabel('I am y') # y lable y
#
# new_ticks = np.linspace(-1, 2, 5) # -1 2
# print(new_ticks)
# plt.xticks(new_ticks) # ( )x ticks
#
# plt.yticks([-2, -1.8, -1, 1.22, 3],[r'really\ bad', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
# # $really$
# # \alpha ( )α
# plt.show()
#
#
# x = np.linspace(-3, 3, 50)
# y1 = 2*x + 1
# y2 = x**2
#
# plt.figure()
# plt.plot(x, y2)
# plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
# plt.xlim((-1, 2))
# plt.ylim((-2, 3))
#
# new_ticks = np.linspace(-1, 2, 5)
# plt.xticks(new_ticks)
# plt.yticks([-2, -1.8, -1, 1.22, 3],['$really\ bad$', '$bad$', '$normal$', '$good$', '$really\ good$'])
#
# # gca = 'get current axis'
# # spines ,
# ax = plt.gca()
# ax.spines['right'].set_color('none') # none
# ax.spines['top'].set_color('none')
#
#
# #
# ax.xaxis.set_ticks_position('bottom') # x bottom( )
#
# ax.spines['bottom'].set_position(('data', 0)) # bottom
#
# ax.yaxis.set_ticks_position('left') # y left( )
# # :outward,axes,data
# ax.spines['left'].set_position(('data',0))
# plt.show()
#
#
# x = np.linspace(-3, 3, 50)
# y1 = 2*x + 1
# y2 = x**2
#
# plt.figure()
# #set x limits
# plt.xlim((-1, 2))
# plt.ylim((-2, 3))
#
# # set new sticks
# new_sticks = np.linspace(-1, 2, 5)
# plt.xticks(new_sticks)
# # set tick labels
# plt.yticks([-2, -1.8, -1, 1.22, 3],
# [r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
#
# # set line syles
# l1, = plt.plot(x, y1, label='linear line') # ‘ ’ legend
# l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='square line')
#
# plt.legend(loc='upper right') # loc
#
# #
# plt.legend(handles=[l1, l2], labels=['up', 'down'], loc='best')
# plt.show()
# # ’loc’ ,’best’ , :
# # 'best': 0,
# # 'upper right': 1,
# # 'upper left': 2,
# # 'lower left': 3,
# # 'lower right': 4,
# # 'right': 5,
# # 'center left': 6,
# # 'center right': 7,
# # 'lower center': 8,
# # 'upper center': 9,
# # 'center': 10,
#
#
# #
# x = np.linspace(-3, 3, 50)
# y = 2*x + 1
#
# plt.figure(num=1, figsize=(8, 5),)
# plt.plot(x, y,)
#
# #
# ax = plt.gca()
# ax.spines['right'].set_color('none')
# ax.spines['top'].set_color('none')
# ax.spines['top'].set_color('none')
# ax.xaxis.set_ticks_position('bottom')
# ax.spines['bottom'].set_position(('data', 0))
# ax.yaxis.set_ticks_position('left')
# ax.spines['left'].set_position(('data', 0))
#
# # scatter
# # plot
# x0 = 1
# y0 = 2*x0 + 1
# plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5) # k--
# # set dot styles
# plt.scatter([x0, ], [y0, ], s=50, color='b')
#
# # annotate 1
# # , ,xy , , , , ( , )
# plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30),
# textcoords='offset points', fontsize=16,
# arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"))
#
# # text 2
# # , ,sigma(_i) , ,
# plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma\ \alpha_t$',
# fontdict={'size': 16, 'color': 'r'})
#
# plt.show()
#
#
# #
# x = np.linspace(-3, 3, 50)
# y = 0.1*x
#
# plt.figure()
# # plt 2.0.2 , zorder plot z
# plt.plot(x, y, linewidth=10, zorder=1)
# plt.ylim(-2, 2)
# ax = plt.gca()
# ax.spines['right'].set_color('none')
# ax.spines['top'].set_color('none')
# ax.spines['top'].set_color('none')
# ax.xaxis.set_ticks_position('bottom')
# ax.spines['bottom'].set_position(('data', 0))
# ax.yaxis.set_ticks_position('left')
# ax.spines['left'].set_position(('data', 0))
#
# #
# for label in ax.get_xticklabels() + ax.get_yticklabels():
# label.set_fontsize(12)
# # plt 2.0.2 , zorder plot z
# # , , ,
# label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.7, zorder=2))
# plt.show()
#
#
# # , 1024
# n = 1024 # data size
# X = np.random.normal(0, 1, n) # X # 0, 1, 1024
# Y = np.random.normal(0, 1, n) # Y
# T = np.arctan2(Y,X) # for color value
#
# plt.scatter(X, Y, s=75, c=T, alpha=.5) # , ,(colormap) ,
#
# plt.xlim(-1.5, 1.5)
# plt.xticks(()) # ignore xticks
# plt.ylim(-1.5, 1.5)
# plt.yticks(()) # ignore yticks
#
# plt.show()
#
#
# # 12 ,X 0 11 ,
# # Y 。 plt.bar, X Y:
# n = 12
# X = np.arange(n)
# Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
# Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
#
# # plt.bar(X, +Y1) #
# # plt.bar(X, -Y2) #
#
# plt.xlim(-.5, n)
# plt.xticks(())
# plt.ylim(-1.25, 1.25)
# plt.yticks(())
#
#
# # facecolor ,edgecolor ,
# plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
# plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
#
# # plt.text ( ) , %.2f ,
# # ha='center', ( ) va='bottom':
# for x, y in zip(X, Y1):
# # ha: horizontal alignment
# # va: vertical alignment
# plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
#
# for x, y in zip(X, Y2):
# # ha: horizontal alignment
# # va: vertical alignment
# plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')
#
# plt.show()
#
#
# #
# # (x,y) , 256 。
# # height function f(x,y) 。 x, y [-3,3] 256
# # meshgrid x y , :
# def f(x,y):
# # the height function
# return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)
#
# n = 256
# x = np.linspace(-3, 3, n)
# y = np.linspace(-3, 3, n)
# X,Y = np.meshgrid(x, y) # , x,y
#
# # 。 plt.contourf ,
# # :X, Y, f(X,Y)。 0.75
# # f(X,Y) color map
# # use plt.contourf to filling contours
# # X, Y and value for (X,Y) point
# plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot) # cmap = colormap [hot,cool ]
#
# # 。 plt.contour 。 :X, Y, f(X,Y)。
# # , 0.5。 , , Label:
# # use plt.contour to add contour lines
# C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)
# # 0 2
# # 8 10
#
# #
# # ,8 , 10 。 0, 。
# # Label,inline Label , 10。 :
# plt.clabel(C, inline=True, fontsize=10)
# plt.xticks(())
# plt.yticks(())
#
# plt.show()
#
#
# #
# # , 。
# # 3x3 2D-array , pixel。
# a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
# 0.365348418405, 0.439599930621, 0.525083754405,
# 0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
#
# # imageshow , ,color map , [lower,upper] ,
# # : http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
# plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower')
#
# # colorbar
# # colorbar , shrink , colorbar 92%:
# plt.colorbar(shrink=.92)
#
# plt.xticks(())
# plt.yticks(())
# plt.show()
#
#
# #
# # from mpl_toolkits.mplot3d import Axes3D
#
# # , 3D
# fig = plt.figure()
# ax = Axes3D(fig)
#
# # X Y , X Y 。
# # (X, Y) 。
# # X, Y value
# X = np.arange(-4, 4, 0.25)
# Y = np.arange(-4, 4, 0.25)
# X, Y = np.meshgrid(X, Y) # x-y
# R = np.sqrt(X ** 2 + Y ** 2)
# # height value
# Z = np.sin(R)
#
# # , colormap rainbow ,
# # XY 。
# # , 1( ),5( ),
# ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
#
# #
# # XY :
# # , z , 0 2 ,
# ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))
#
# plt.show()
#
#
# # figure
# plt.figure()
#
# # plt.subplot 。 plt.subplot(2,2,1) 2 2 , 1.
# # plt.plot([0,1],[0,1]) 1 .
# plt.subplot(2,2,1)
# plt.plot([0,1],[0,1])
#
# # plt.subplot(2,2,2) 2 2 , 2.
# # plt.plot([0,1],[0,2]) 2 .
# plt.subplot(2,2,2)
# plt.plot([0,1],[0,2])
#
# # plt.subplot(2,2,3) 2 2 , 3.
# # plt.subplot(2,2,3) plt.subplot(223), matplotlib .
# # plt.plot([0,1],[0,3]) 3 .
# plt.subplot(223)
# plt.plot([0,1],[0,3])
#
# # plt.subplot(224) 2 2 , 4.
# # plt.plot([0,1],[0,4]) 4 .
# plt.subplot(224)
# plt.plot([0,1],[0,4])
#
# plt.show() #
#
#
# subplot
# 1:subplot2grid
# plt.figure()
#
# ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
# ax1.plot([1, 2], [1, 2]) #
# ax1.set_title('ax1_title') #
#
# ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
# ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
# ax4 = plt.subplot2grid((3, 3), (2, 0))
# ax5 = plt.subplot2grid((3, 3), (2, 1))
#
# ax4.scatter([1, 2], [2, 2])
# ax4.set_xlabel('ax4_x')
# ax4.set_ylabel('ax4_y')
#
# plt.show()
# 2 gridspec
# import matplotlib.gridspec as gridspec
# plt.figure()
# gs = gridspec.GridSpec(3, 3)
#
# ax6 = plt.subplot(gs[0, :])
# ax7 = plt.subplot(gs[1, :2])
# ax8 = plt.subplot(gs[1:, 2])
# ax9 = plt.subplot(gs[-1, 0])
# ax10 = plt.subplot(gs[-1, -2])
# plt.show()
# 3 subplots
# f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)
#
# ax11.scatter([1,2], [1,2])
#
# plt.tight_layout()
# plt.show()
#
#
# # figure
# fig = plt.figure()
#
# #
# x = [1, 2, 3, 4, 5, 6, 7]
# y = [1, 3, 4, 2, 5, 8, 6]
# # figure 0.1 10%
# left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# # figure , r(red), title:
# ax1 = fig.add_axes([left, bottom, width, height])
# ax1.plot(x, y, 'r')
# ax1.set_xlabel('x')
# ax1.set_ylabel('y')
# ax1.set_title('title')
# #
# left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
# ax2 = fig.add_axes([left, bottom, width, height])
# ax2.plot(y, x, 'b')
# ax2.set_xlabel('x')
# ax2.set_ylabel('y')
# ax2.set_title('title inside 1')
# # ,
# plt.axes([0.6, 0.2, 0.25, 0.25])
# plt.plot(y[::-1], x, 'g') # y
# plt.xlabel('x')
# plt.ylabel('y')
# plt.title('title inside 2')
#
# plt.show()
#
#
# x = np.arange(0, 10, 0.1)
# y1 = 0.05 * x**2
# y2 = -1 * y1
# # figure ax1:
# fig, ax1 = plt.subplots()
#
# # ax1 twinx() , ax2:
# ax2 = ax1.twinx()
# # , y1, y2 ax1, ax2 :
# ax1.plot(x, y1, 'g-') #
# ax1.set_xlabel('X data')
# ax1.set_ylabel('Y1 data', color='g')
# ax2.plot(x, y2, 'b-') # blue
# ax2.set_ylabel('Y2 data', color='b')
# plt.show()
#
#
# from matplotlib import animation
# fig, ax = plt.subplots()
# # 0~2π :
# x = np.arange(0, 2*np.pi, 0.01)
# line, = ax.plot(x, np.sin(x))
# # animate, x y , i :
# def animate(i):
# line.set_ydata(np.sin(x + i/10.0))
# return line,
# # , init:
# def init():
# line.set_ydata(np.sin(x))
# return line,
#
# # , FuncAnimation 。 :
#
# # fig figure
# # func , animate
# # frames ,
# # init_func , init
# # interval , ms
# # blit , 。 True, mac False,
# ani = animation.FuncAnimation(fig=fig,
# func=animate,
# frames=100,
# init_func=init,
# interval=20,
# blit=True)
#
# plt.show()
# # , mp4 ,
# # ffmpeg mencoder, matplotlib animation api:
# # ani.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
#