numpy & pandas & matplotlib 学习笔记

3/9/2018

c >>>编写>>> numpy >>>升级>>> pandas

https://www.bilibili.com/video/av16378934/

NO.3 - NO.5

 

3/10/2018

NO.6

 

3/11/2018

NO.10

 

3/12/2018

NO.17

 

3/13/2018

NO.18

 

补充了一些pandas的操作

 

#2.2 create np array
import numpy as np

# array = np.array([[1,2,3],[2,3,4]],dtype = np.float)
# print(array)
# print(array.dtype)
# print('NO. of dim:',array.ndim)
# print('shape:',array.shape)
# print('size:',array.size)

# brray = np.zeros((3,4))
# print(brray)
# crray = np.ones((3,4),dtype=np.int16)
# print(crray)
# drray = np.empty((3,4))
# print(drray)
# erray = np.arange(10,20,2)
# print(erray)
# frray = np.arange(12).reshape((3,4))
# print(frray)
# grray = np.linspace(0,10,21)
# print(grray)













#2.3 np basic calculation I
# a = np.linspace(10,20,6)
# b = np.arange(6)
# c = a - b #addition, substraction
# print(a,'\n',b,'\n',c)

# d = b**2 #power
# f = a*b #multiplication
# print(d, '\n',f)

# g = np.sin(10*a*np.pi/180) #trigonometric function, exponential function
# print(g)

# print(b<3) #comparsion
# print(b == 3)

# h = np.array([[1,1],
#               [0,1]])
# i = np.arange(1,5,1).reshape((2,2))
# print(h*i) #multiplication one by one
# print(np.dot(h,i)) #matrix dot product
# print(h.dot(i))

# j = np.random.random((2,4)) #random number in [0,1)。 ↓summation, maximum, minimum
# print(j)
# print(np.sum(j,axis = 1)) #axis = 1 -> sum of every row, axis = 0 -> sum of every column
# print(np.max(j))
# print(np.max(j,axis = 1)) #axis = 1 -> max in every row, axis = 0 -> max in every column
# print(np.min(j))













#2.4 numpy basic calculation II
# A = np.random.random((3,4))
# print(A)
# print(np.argmin(A)) #seek for min & max number index
# print(np.argmax(A))
# print(np.mean(A,axis = 1)) #axis = 1 -> mean of every row, axis = 0 -> mean of every column
# print(A.mean())
# print(np.average(A))

# A = np.arange(15,0,-1).reshape(3,5)
# print(np.cumsum(A)) #accumulation between the two adjacent number, output a 1*15 array
# print(np.diff(A)) #difference between the two adjacent number, output a 3*4 = 3*(5-1)

# print(np.transpose(np.nonzero(A))) #find the coordinate value of the nonzero numbers.

# print(np.sort(A)) #sort every sub-array in A
# print(np.transpose(A)) #transpose type I
# print(A.T) #transpose type II
# print((A.T).dot(A))

# print(np.clip(A,None,9)) #turn any number < None  to it's original value, turn any number > 9 to 9














#2.5 index in numpy
# A = np.arange(3,15).reshape((3,4))
# print(A)
# print(A[2][1])
# print(A[2,1])
# print(A[2,:])
# print(A[:,1])
# print(A[1,1:2])

# for row in A:
#     print(row)
#     print(np.shape(row)) # vector is a row one by default(??really??)
# print(A.T)
# for column in A.T:
#     print(np.transpose(column).reshape(3,1))
#     print(np.shape(column)[0]) #np.shape is a list, probably with more than one dimension

# print(A.flatten()) # turn N-dimension list A into a vector
# for i in A.flat: # Abbreviation of turning N-dimension list A into a vector when A is iterated.
#     print(i)














#2.6 merging array in numpy
# A = np.array([1,1,1])
# B = np.array([2,2,2])

# print(np.vstack((A,B))) #vertically stack
# print(np.hstack((A,B))) #horizontally stack

# print(A.shape)
# print(A[np.newaxis,:].shape) #np.newaxis before comma means reshaping A to (1,old dimensions)
# print(A[np.newaxis,:] == A)
# print(A[np.newaxis,:] is A)
# print(A[:,np.newaxis].shape) #np.newaxis after comma means reshaping A to (old dimensions,1)
# print(A[:,np.newaxis])

# A = np.array([1,1,1])[:,np.newaxis] #turn a vector into a column vector
# B = np.array([2,2,2])[:,np.newaxis]

# print(np.vstack((A,B))) #vertically stack
# print(np.hstack((A,B))) #horizontally stack

# print(np.hstack((A,A,B,B))) #multiple stack

# C = np.concatenate((A,A,B,B),axis = 0) #define the stacking mode, giving axis value at the last step














#2.7 Partition array in numpy
# A = np.arange(5,17).reshape(3,4)
# print(A)


# print(np.split(A,4,axis = 1)) #split array A uniformly into 4 sub-arrays
# print(np.vsplit(A,4)) #split array A uniformly into 4 sub-arrays
# print(np.split(A,[1],axis = 1)) #split array A into [0,1) and [1,A.shape[1]) two sub-arrays
# print(np.array_split(A,3,axis = 1)) #split array A nonuniformly into 3 sub-arrays














#2.8 Copy & Deep Copy in Numpy
# a = np.arange(3,12,3)
# b = a
# c = a[:] #if a is a np array, this command will only shallowly copy a to c
# d = a.copy()
# a[0] = 0
# print(a,'\n',b,'\n',c,'\n',d)













#3.1 Pandas -  A Brief Introduction
import pandas as pd

# s = pd.Series([1,3,6,np.nan,44,1]) #one-dimensional pd data 
# print(s)

# dates = pd.date_range('20180311',periods = 6) #one-dimensional pd date data 
# print(dates)

# df = pd.DataFrame(np.random.rand(6,4),index = dates,columns = ['a','b','c','d'])
# print(df) #two-dimensional pd data, with index and columns names defined 

# df1 = pd.DataFrame(np.arange(12).reshape(3,4))
# print(df1) #two-dimensional pd data, without any index and column defination

# df2 = pd.DataFrame({
# 'A':1.,
# 'B':pd.Timestamp('20180312'),
# 'C':pd.Series(1,index = list(range(4)),dtype = 'float32'),
# 'D':np.array([3]*4,dtype = 'int32'),
# 'E':pd.Categorical(["test","train","test","train"]), #what is Categorical in pandas???
# 'F':'foo'
# }) #two-dimensional pd data, defined by a dictionary
# print(df2)
# print(df2.dtypes)
# print(df2.index)
# print(df2.columns)
# print(df2.values)
# print(df2.describe())
# print(df2.T)
# print(df2.sort_index(axis = 0, ascending = False)) #axis = 1 → sort column, axis = 0 → sort index
# print(df2.sort_values(by = 'E'))













#3.2 Selecting data from Pandas
# dates = pd.date_range('20130101',periods = 6)
# df = pd.DataFrame(np.arange(24).reshape(6,4),index = dates,columns = list('ABCD'))

# print(df['A'])
# print(df['A'] is df.A)
# print(df[0:3])
# print(df['20130102':'20130104'])

# print(df)
# print(df.loc['20130102',['A','C']]) #loc treats only label names
# print(df.iloc[1,[0,2]]) # iloc treats only label numbers
# print(df.ix[1,['A','C']]) #ix is the mixture of loc and iloc

# co = ['a','b','c']
# df = pd.DataFrame(np.random.rand(4,3),columns = co)
# print(df)
# print(df.query('a>0.6 and a<0.7'))


# student = pd.read_csv('pyread.csv')
# print(student.head(3))
# print(student.ix[:,['name','gender']].tail())
# print(student[(student['gender'] == 'Female') & (student['age'] < 24)])













#3.3 Setting values in Pandas
# dates = pd.date_range('20130101',periods = 6)
# df = pd.DataFrame(np.arange(24).reshape(6,4),index = dates,columns = list('ABCD'))

# df.ix[2,2] = 1111
# df.ix['20130101',1] = 222
# df.A[df.A > 4] = 0
# print(df)

# df = pd.DataFrame(np.arange(24).reshape(6,4),index = dates,columns = list('ABCD'))
# df[df.A > 4] = 0
# print(df)

# df = pd.DataFrame(np.arange(24).reshape(6,4),index = dates,columns = list('ABCD'))
# df['CC'] = pd.Series(np.arange(1,7),index = dates)
# df['E'] = np.nan
# print(df)
# print(df.sort_index(axis = 1))













# 3.4 Treating those missed values
# dates = pd.date_range('20130101',periods = 6)
# df = pd.DataFrame(np.arange(24).reshape(6,4),index = dates,columns = list('ABCD'))
# df.iloc[0,1] = np.nan
# df.iloc[3,3] = np.nan
# print(df)
# print(df.dropna(axis = 0, how = 'any')) # if how = 'any', then drop out any row that has a nan item,
#                                         # if how = 'all', only drop out rows that all items are nans.
# print(df.dropna(axis = 1))
# print(df.fillna(value = -100))
# print(df.isnull())
# print(np.any(df.isnull()) == True)












#3.5 Import and Export values
# data = pd.read_csv('pyread.csv')
# print(type(data))
# #data.index = [2,3,4,5]
# data.to_csv('pyto.csv')











#3.6 Concatenating DataFrame Object and Appending DataFrame

# df1 = pd.DataFrame(np.ones((3,4))*0,columns = list('abcd'))
# df2 = pd.DataFrame(np.ones((3,4))*1,columns = list('abcd'))
# df3 = pd.DataFrame(np.ones((3,4))*2,columns = list('abcd'))

# print(df1)
# print(df2)
# print(df3)

# print(pd.concat([df1,df2,df3],axis = 0))
# #如果简单点来说,就是0轴匹配的是index, 涉及上下运算;1轴匹配的是columns, 涉及左右运算。
# print(pd.concat([df1,df2,df3],axis = 0,ignore_index = True))

# df1 = pd.DataFrame(np.ones((3,4))*0,columns = list('abcd'),index = [1,2,3])
# df2 = pd.DataFrame(np.ones((3,4))*1,columns = list('bcde'),index = [2,3,4])

# print(pd.concat([df1,df2])) # equals to → print(pd.concat([df1,df2],join = 'outer')) → df1 ∪ df2
# print(pd.concat([df1,df2],join = 'inner',ignore_index = True)) # df1 ∩ df2
# print(pd.concat([df1,df2],axis = 1,join_axes = [df1.index])) #ignore index not in df1


# df1 = pd.DataFrame(np.ones((3,4))*0,columns = list('abcd'))
# df2 = pd.DataFrame(np.ones((3,4))*1,columns = list('abcd'))
# df3 = pd.DataFrame(np.ones((3,4))*2,columns = list('abcd'))

# res1 = df1.append(df2,ignore_index = True)
# print(res1)
# res2 = df1.append([df1,df2],ignore_index = False)
# print(res2)

# s1 = pd.Series([1,2,3,4],index = list('abcd'))
# res3 = df1.append(s1,ignore_index = True)
# print(res3)











#3.7 Merging in Pandas

# left = pd.DataFrame({
#     'key': ['K0', 'K1', 'K2', 'K3'],
#     'A': ['A0', 'A1', 'A2', 'A3'],
#     'B': ['B0', 'B1', 'B2', 'B3']
# })
# right = pd.DataFrame({
#     'key': ['K0', 'K1', 'K2', 'K3'],
#     'C': ['C0', 'C1', 'C2', 'C3'],
#     'D': ['D0', 'D1', 'D2', 'D3']
# })
# print(left)
# print(right)

# res = pd.merge(left,right,on = 'key') #merge by column(axis = 1)
# print(res)



# left = pd.DataFrame({
#     'key1': ['K0', 'K0', 'K1', 'K2'],
#     'key2': ['K0', 'K1', 'K0', 'K1'],
#     'A': ['A0', 'A1', 'A2', 'A3'],
#     'B': ['B0', 'B1', 'B2', 'B3']
# })
# right = pd.DataFrame({
#     'key1': ['K0', 'K1', 'K1', 'K2'],
#     'key2': ['K0', 'K0', 'K0', 'K0'],
#     'C': ['C0', 'C1', 'C2', 'C3'],
#     'D': ['D0', 'D1', 'D2', 'D3']
# })
# print(left)
# print(right)

# res = pd.merge(left,right,on = ['key1','key2'],how = 'inner') #how = 'inner' by default
# print(res) #how = ['inner','outer','left','right']

# res = pd.merge(left,right,on = ['key1','key2'],how = 'outer')
# print(res)

# res = pd.merge(left,right,on = ['key1','key2'],how = 'left')
# print(res)

# res = pd.merge(left,right,on = ['key1','key2'],how = 'right')
# print(res)



# df1 = pd.DataFrame({'col1':[0,1], 'col_left':['a','b']})
# df2 = pd.DataFrame({'col1':[1,2,2],'col_right':[2,2,2]})
# print(df1)
# print(df2)
# res = pd.merge(df1, df2, on='col1', how='outer', indicator=True)
# print(res) #changing the indicator column's name by assigning indicator "??a_new_name??"



# left = pd.DataFrame({
#     'A': ['A0', 'A1', 'A2'],
#     'B': ['B0', 'B1', 'B2']},
#     index=['K0', 'K1', 'K2'])
# right = pd.DataFrame({
#     'C': ['C0', 'C2', 'C3'],
#     'D': ['D0', 'D2', 'D3']},
#     index=['K0', 'K2', 'K3'])
# print(left)
# print(right)
# # left_index and right_index
# res = pd.merge(left, right, left_index=True, right_index=True, how='outer')
# print(res) #merge by index (axis = 0)
# res = pd.merge(left, right, left_index=True, right_index=True, how='inner')
# print(res)


# boys = pd.DataFrame({
#     'k': ['K0', 'K1', 'K2'], 
#     'age': [1, 2, 3]
# })
# girls = pd.DataFrame({
#     'k': ['K0', 'K0', 'K3'], 
#     'age': [4, 5, 6]
# })
# print(boys)
# print(girls)
# res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girl'], how='inner')
# print(res)
# res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girl'], how='outer')
# print(res)










#3.8 Pandas Plot
import matplotlib.pyplot as plt

# data = pd.Series(np.random.randn(1000),index = np.arange(1000))
# data = data.cumsum()
# data.plot()
# plt.show()

# data2 = pd.DataFrame(np.random.randn(1000,4),index = np.arange(1000),columns = list('ABCD'))
# data2 = data2.cumsum()
# ax1 = data2.plot.scatter(x = 'A', y = 'B', color = 'DarkBlue',label = 'Class 1')
# data2.plot.scatter(x = 'A', y = 'C', color = 'DarkGreen',label = 'Class 2',ax = ax1)

# plt.show()

#plot methods: 'bar' 'hist' 'box' 'kde' 'area' 'scatter' 'hexbin' 'pie'










#4.1 Arithmetic in Pandas

# df1 = pd.DataFrame(np.random.rand(3,4),columns = list('abcd'))
# df2 = pd.DataFrame(np.ones((3,4))*2,columns = list('bcde'))

# print(df1+df2)
# print(df1/df2)









#4.2 Searching Data in Pandas Series
# np.random.seed(1234)
# d1 = pd.Series(2*np.random.randn(10)+3)
# print(d1)
# d11 = pd.Series(np.random.normal(loc = 3,scale = 2,size = 10))
# print(d11)
# d2 = pd.Series(np.random.f(2,4,size = 10)) #Draw samples from an F distribution
# print(d2)
# d3 = pd.Series(np.random.randint(1,100,size = 10))
# print(d3)
# d1.count() #非空元素计算
# d1.min() #最小值
# d1.max() #最大值
# d1.idxmin() #最小值的位置,类似于R中的which.min函数
# d1.idxmax() #最大值的位置,类似于R中的which.max函数
# d1.quantile(0.1) #10%分位数
# d1.sum() #求和
# d1.mean() #均值
# d1.median() #中位数
# d1.mode() #众数
# d1.var() #方差
# d1.std() #标准差
# d1.mad() #平均绝对偏差
# d1.skew() #偏度
# d1.kurt() #峰度
# d1.describe() #一次性输出多个描述性统计指标










#4.3 Delete Data in Pandas
df = pd.DataFrame(np.arange(48).reshape(8,6),columns = list('abcdef'))
print(df)
print(df.drop([0,3])) #drop out row 0 and 3
print(list(df[df['a'] > 15].index))
print(df.drop(df[df['a'] > 15].index, axis = 0)) #delete certain items with some boolean rules
print(df.drop(['b','d'],axis = 1).head())
print(df.shape[0]) #total row count
print(df.shape[1]) #total column count

 

 

https://www.bilibili.com/video/av16378354/

NO.3开始

看到NO.7

 

3/14/2018

 

NO.19 matplotlib视频结束

 

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D as ad

#2.1 Basic Usage

# x = np.linspace(-1,1,50)
# y = 2*x + 1
# plt.plot(x,y) #plot methods: 'bar' 'hist' 'box' 'kde' 'area' 'scatter' 'hexbin' 'pie'
# plt.show()


# Domain of Definition

x = np.linspace(-3,3,50) 


#Curve Equations

y = 2*x + 1
z = x**2


#Try to plot

#plt.figure() #  ← ← ← ← ← ← Fig1 (the first fig)
#plt.plot(x,y) #  ← ← ← ← ← ← choose which curve to be plotted in Fig1









#2.2 Edit Figure Parameter

plt.figure(num = 99,figsize = (8,8)) #  ← ← ← ← ← ← Fig99 (the second fig)
# ↑ ↑ ↑ ↑ ↑ ↑ (num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None,
# ↑ ↑ ↑ ↑ ↑ ↑ frameon=True, FigureClass=Figure, clear=False, **kwargs










#2.3 Edit Axes Text Parameter

plt.xlim((-1,2)) #  ← ← ← ← ← ← horizontal axis's value range
plt.ylim((-2,3)) #  ← ← ← ← ← ← vertical axis's value range


plt.xlabel('XXXX')
plt.ylabel("I'm YYYYY")


new_ticks = np.linspace(-1,2,5)
plt.xticks(new_ticks) #  ← ← ← ← ← ← change the ticks of the horizontal axis
plt.yticks([-2,-1.8,-1,1.22,5],[r'$really\ bad$', r'$bad\ \alpha$', 'normal','good','really good'])
# ↑ ↑ ↑ ↑ ↑ ↑ r'$$' is to change the font of the ticks to a more mathematical one
# ↑ ↑ ↑ ↑ ↑ ↑ Caution: between the two $, space should have a \ before it.
# ↑ ↑ ↑ ↑ ↑ ↑ Use \alpha to type Greek letters










#2.4 Set Axes Position

ax = plt.gca() #  ← ← ← ← ← ← gca = 'get current axis'
ax.spines['right'].set_color('none') #  ← ← ← ← ← ← spine is 脊椎, meaning the boder lines here.
ax.spines['top'].set_color('none') #  ← ← ← ← ← ← color 'none' means to remove the boder line.
# ↑ ↑ ↑ ↑ ↑ ↑ spines are 'top','bottom','right','left'
ax.xaxis.set_ticks_position('bottom') #  ← ← ← ← ← ← set x axis to bottom spine
ax.yaxis.set_ticks_position('left') #  ← ← ← ← ← ← set y axis to left spine
ax.spines['bottom'].set_position(('data',0)) #  ← ← ← ← ← ← set bottom spine to y = 0
ax.spines['left'].set_position(('data',0)) #  ← ← ← ← ← ← set bottom spine to x = 0
# ↑ ↑ ↑ ↑ ↑ ↑ set_position(('axes',??))
# ↑ ↑ ↑ ↑ ↑ ↑ set_position(('outward',??))










#2.5 Legend

# ↓ ↓ ↓ ↓ ↓ ↓ Using plt.plot(x,z) without 'line1, = '(the returned value) is also okay.
line1, = plt.plot(x,z,color = 'red',linewidth = 1.0, linestyle = '--',label = 'down')
# ↑ ↑ ↑ ↑ ↑ ↑ choose line parameter. The comma after line is to enable 'handles' in plt.legend.
line2, = plt.plot(x,y,label = 'up') # ↑ ↑ ↑ ↑ ↑ ↑ line parameter: color, linewidth, linestyle, label
plt.legend(handles = [line1,line2,], labels = ['aaa','bbb'], loc = 'best')
# ↑ ↑ ↑ ↑ ↑ ↑ will plot out the legend
# ↑ ↑ ↑ ↑ ↑ ↑ loc:['best','upper right','center? left','lower right',etc.], where to put the legend
# ↑ ↑ ↑ ↑ ↑ ↑ labels = ['aaa','bbb'] here will overwrite label = 'up' and label = 'down'
# ↑ ↑ ↑ ↑ ↑ ↑ handles = [line1,line2,] or handles = [line2,] are both okay.









#2.6 Annotation

x0 = 1
y0 = x0*2 + 1
plt.scatter(x0,y0,s = 50, c = 'b',) 
# ↑ ↑ ↑ ↑ ↑ ↑ plot a point. s means size. Both c or color are okay. b means blue.
plt.plot([x0,x0],[y0,0],'k--', lw = 2.5)
# ↑ ↑ ↑ ↑ ↑ ↑ plot a line. k means black, and -- means the linestyle (dashed). lw means linewidth.
# ↑ ↑ ↑ ↑ ↑ ↑ [x0,x0],[y0,0] means the two ends of the line is [x0,y0] and [x0,0]

# ↓ ↓ ↓ ↓ ↓ ↓ Annotation Method I
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"))

# ↓ ↓ ↓ ↓ ↓ ↓ Annotation Method II
plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
         fontdict={'size': 16, 'color': 'r'})









#2.7 Tick Visibility
for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(12)
    label.set_bbox(dict(facecolor = 'white', edgecolor = 'None', alpha = 0.7))
    # ↑ ↑ ↑ ↑ ↑ ↑ alpha = visibility ∈ [0,1]









#3.1 Plot Scatter Chart

plt.figure(num = 100,figsize = (8,8))

n = 1024 # number of the data
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
Co = np.arctan2(Y,X) #for color value


plt.scatter(X,Y,s = 75,c = Co, alpha = 0.5) # alpha = visibility ∈ [0,1]

plt.xlim((-1.5,1.5))
plt.ylim((-1.5,1.5))

plt.xticks() # remove x ticks
plt.yticks() # remove y ticks









#3.2 Plot Bar Chart

plt.figure(num = 101,figsize = (8,8))

n = 12
x = np.arange(n)
y1 = (1-x/float(n))*np.random.uniform(0.5,1,n) #uniform distribution
y2 = (1-x/float(n))*np.random.uniform(0.5,1,n)

plt.bar(x,+y1,facecolor = '#9999ff',edgecolor = 'white')
plt.bar(x,-y2,facecolor = '#ff9999',edgecolor = 'white')

for a,b in zip(x,y1): # zip(x,y1) enables value transforming simultaneously
    plt.text(a,b + 0.05, '%.2f' % b, ha = 'center',va = 'bottom') 
    #'%.2f' % y means 保留两位小数的y。ha means horizontal alignment。va means vertical alignment(对齐方式)。

for a,b in zip(x,y2): # zip(x,y1) enables value transforming simultaneously
    plt.text(a,- b - 0.05, '-%.2f' % b, ha = 'center',va = 'top') 
    #'%.2f' % y means 保留两位小数的y。ha means horizontal alignment。va means vertical alignment(对齐方式)。


plt.xlim(-.5, n)
plt.xticks(())
plt.ylim(-1.25, 1.25)
plt.yticks(())









#3.3 Plot Contour Chart

plt.figure(num = 102,figsize = (8,8))

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)

#plot contour filling
plt.contourf(X,Y,f(X,Y),8,alpha = 0.75,cmap = 'hot') # 8 means eight kinds of heights.
#cmap is the mapping between data and color. cmp = plt.cm.hot, or plt.cm.cold is also okay.

#plot contour lines
C = plt.contour(X,Y,f(X,Y),8,colors = 'black',linewidth = 0.5)

#add label
plt.clabel(C, inline = True, fontsize = 10) #inline = True means lines will avoid pass through labels









#3.4 Plot Image

plt.figure(num = 103,figsize = (8,8))

a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
              0.365348418405, 0.439599930621, 0.525083754405,
              0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)

plt.imshow(a,interpolation = 'nearest',cmap = 'bone',origin = 'lower') #cmap = plt.cm.bone is also okay.
# origin = 'upper' will turn the image to the correct direction:
# http://matplotlib.org/examples/pylab_examples/image_origin.html
# interpolation's value:
# http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html

plt.colorbar() # use this command to show collor bar
# use plt.colorbar(shrink = 0.9) to shrink the colorbar









#3.5 Plot 3D Data

fig3d = plt.figure(num = 103,figsize = (8,8))
ax = ad(fig3d)

x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
X,Y = np.meshgrid(x,y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

ax.plot_surface(X,Y,Z, rstride = 1, cstride = 1, cmap = plt.get_cmap('rainbow'))
# (rstride,cstride) means the (row,column) stride

ax.contourf(X,Y,Z,zdir = 'z', offset = -10, cmap = 'rainbow') #zdir means the direction of projecting
# offset means the offset of contour figure towards the axis

ax.set_zlim(-2,2)









#4.1 Subplot zero

plt.figure(num = 104,figsize = (8,8))

#http://blog.csdn.net/yuan1125/article/details/69934785

plt.subplot(4,2,1)
plt.plot([0,1],[0,1])

plt.subplot(4,2,2)
plt.plot([0,1],[0,2])

plt.subplot(4,2,3)
plt.plot([0,1],[0,3])

plt.subplot(4,2,4)
plt.plot([0,1],[0,4])

plt.subplot(4,1,3)
plt.plot([0,1],[0,5])

plt.subplot(4,2,7)
plt.plot([0,1],[0,6])

plt.subplot(4,2,8)
plt.plot([0,1],[0,7])

# another expression

# ax1 = plt.subplot(4,2,1)
# ax1.plot([0,1],[0,1])

# ax2 = plt.subplot(4,2,2)
# ax2.plot([0,1],[0,2])

# ax3 = plt.subplot(4,2,3)
# ax3.plot([0,1],[0,3])

# ax4 = plt.subplot(4,2,4)
# ax4.plot([0,1],[0,4])

# ax5 = plt.subplot(4,1,3)
# ax5.plot([0,1],[0,5])

# ax6 = plt.subplot(4,2,7)
# ax6.plot([0,1],[0,6])

# ax7 = plt.subplot(4,2,8)
# ax7.plot([0,1],[0,7])







#4.1 Subplot I, II, III

# I subplot2grid

plt.figure(num = 105,figsize = (8,8))

ax1 = plt.subplot2grid((3,3),(0,0),colspan = 3, rowspan = 1)
ax1.plot([0,1],[0,1])
ax1.set_title('ax1_title')

ax2 = plt.subplot2grid((3,3),(1,0),colspan = 2, rowspan = 1)
ax2.plot([0,2],[0,2])
ax2.set_title('ax2_title')

ax3 = plt.subplot2grid((3,3),(2,0),colspan = 1, rowspan = 1)
ax3.plot([0,2],[0,2])
ax3.set_title('ax2_title')

ax4 = plt.subplot2grid((3,3),(2,1),colspan = 1, rowspan = 1)
ax4.plot([0,2],[0,2])
ax4.set_title('ax2_title')

ax5 = plt.subplot2grid((3,3),(1,2),colspan = 1, rowspan = 2)
ax5.plot([0,2],[0,2])
ax5.set_title('ax2_title')

# II gridspec

import matplotlib.gridspec as gridspec

plt.figure(num = 106,figsize = (8,8))

gs = gridspec.GridSpec(3,3)

ax1 = plt.subplot(gs[0,:])
#ax1.plot([0,2],[0,2])
ax2 = plt.subplot(gs[1,:2])
ax3 = plt.subplot(gs[2,0])
ax4 = plt.subplot(gs[2,1])
ax5 = plt.subplot(gs[1:,2])

# III easy to define structure

plt.figure(num = 107,figsize = (8,8))

f,((ax11,ax12),(ax21,ax22)) = plt.subplots(2,2,sharex = True,sharey = True)
ax11.plot([1,2],[1,2])

plt.tight_layout()









#4.3 Picture-in-Picture

fig = plt.figure(num = 107,figsize = (8,8))
x = np.arange(1,8)
y = [1,3,4,2,5,8,6]

left,bottom,width,height = 0.1,0.1,0.8,0.8
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 I')


plt.axes([0.6,.2,.25,.25])
plt.plot(y[::-1],x,'g')
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside II')









#4.4 Primary and Secondary Axis

plt.figure(num = 108,figsize = (8,8))

x = np.arange(0,10,0.1)
y1 = 0.05*x**2
y2 = -y1

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

ax1.plot(x,y1,'g-')
ax2.plot(x,y2,'b--')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1')
ax2.set_ylabel('Y2')









#5.1 Animation
from matplotlib import animation

plt.figure(num = 109,figsize = (8,8))

fg, ax = plt.subplots()
x = np.arange(0,2*np.pi,0.01)
line, = ax.plot(x,np.sin(x))

def animationfunc(i):
    line.set_ydata(np.sin(x + i/100))
    return line,

def animationinit():
    line.set_ydata(np.sin(x))
    return line,

ani = animation.FuncAnimation(fig = fg, func = animationfunc, frames = 100,
                              init_func = animationinit, interval = 20, blit = False)
# frames = 100 means 100 frames(帧). interval = 20 means the time lag between two adjacent frames.
# blit = False means the program will only refresh points that has changes.


















#999.999

plt.show() #  ← ← ← ← ← ←  OK. Let's get started!

 

posted @ 2018-03-09 23:18  ecoflex  阅读(272)  评论(0编辑  收藏  举报