基本用法
import matplotlib.pyplot as plt
import numpy as np"""
2.1基本用法
"""
# x = np.linspace(-1,1,50) #[-1,1]50個點
# #y = 2*x +1
#
# y = x**2
# plt.plot(x,y) #注意:x,y順序不能反
# plt.show()"""
2.2figure圖像
"""# x = np.linspace(-3,3,50)
# y1 = 2*x +1
# y2 = x**2
# # plt.figure() #一個figure對應一張圖
# # plt.plot(x,y1)
#
# plt.figure(num=3,figsize=(8,5))
# #num可以改變這是第幾個figure,不然默認順序排列。figsize是設置輸出的長寬
# plt.plot(x,y2)
# plt.plot(x,y1,color = 'red',linewidth=1.0,linestyle='--')
# #線條的顏色,粗細,線條格式
# plt.show()"""
2.3,2.4 坐標軸設置
"""# x = np.linspace(-3,3,50)
# y1 = 2*x +1
# y2 = x**2
# plt.figure(num=3,figsize=(8,5))
# plt.plot(x,y2)
# plt.plot(x,y1,color = 'red',linewidth=1.0,linestyle='--')
#
# plt.xlim((-1,2)) #設置坐標軸范圍
# plt.ylim((-2,3))
#
# plt.xlabel('I am x') #坐標軸描述
# plt.ylabel('I am y')
#
# #2.3
#
# new_ticks = np.linspace(-1,2,5)
# print(new_ticks)
# plt.xticks(new_ticks) #替換x軸坐標
# plt.yticks([-2,-1.8,-1,1.22,3],
# [r'$really\ bad$',r'$bad$',r'$normal$',r'$good$',r'$really\ good$']) #替換y軸坐標為文字
# #r是正則,$是為了讓字體變好看,在字體中空格可能不識別,需要\+空格 來得到空格#2.4# #gca = 'get current axis'
# ax = plt.gca() #獲得當前坐標軸
# ax.spines['right'].set_color('none') #spines對應的是圖表的邊框,right就是右邊框,set_color('none')就是使右邊框消失
# ax.spines['top'].set_color('none')
# ax.xaxis.set_ticks_position('bottom') #用下面(bottom)坐標軸代替x
# ax.yaxis.set_ticks_position('left')
# ax.spines['bottom'].set_position(('data',-1)) #把x軸綁定在y軸的-1位置
# ax.spines['left'].set_position(('data',0))
#
# plt.show()"""
2.5 Legend圖例
"""# l1,=plt.plot(x,y2,label = 'up')
# #plt.plot其實是有返回值的,為了放入handles中必須l1+逗號
# l2,=plt.plot(x,y1,color = 'red',linewidth=1.0,linestyle='--',label = 'down')
# #plt.legend() #打出來label和圖例
# #個性的圖例
# plt.legend(handles=[l1,l2], labels =['aaa','bbb'],loc = 'best') #打出來label和圖例
# # loc=best可以自動找數據比較少的地方,防止擋住數據
# plt.show()"""
2.6 Annotation標注
"""
#
# 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.xaxis.set_ticks_position('bottom')
# ax.yaxis.set_ticks_position('left')
# ax.spines['bottom'].set_position(('data',0))
# ax.spines['left'].set_position(('data',0))
#
# x0 = 1
# y0 = 2*x0 + 1
# plt.scatter(x0,y0,s=50,color = 'b') #展示(x0,y0)點 ,s = size
# plt.plot([x0,x0],[y0,0],'k--',lw = 2.5) #從[x0,x0]到[y0,0]畫條直線,k=black,--是虛線,lw是粗細
#
# #method 1
# 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'))
# #r'$2x+1=%s$'%y0是指標注的文字,xy=(x0,y0)是指出這個點,xycoords以data值為基準,textcoords是指在xytext的位置上(x+30,y-30)進行描述
#
# #method 2
# plt.text(-3.7,3,r'$This\ is\ the\ some\ text.\mu\ \sigma_i\ \alpha_t$',fontdict={'size':16,'color':'r'})
# plt.show()"""
2.7 tick能見度
"""x = np.linspace(-3,3,50)
y = 0.1*xplt.figure()
plt.plot(x,y,linewidth = 10)
plt.ylim(-2,2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))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是透明度plt.show()