import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
'''
設置繪圖對象
'''
def setFigure(size):
plt.figure(figsize=size)
'''
設置標題
'''
def setTitle(title):
plt.title(title)
'''
設置X軸描述
'''
def setXlabel(xlable):
plt.xlabel(xlable)
'''
設置Y軸描述
'''
def setYlabel(ylable):
plt.ylabel(ylable)
'''
畫線性圖
'''
def plotLineChart(x_axis,y_axis,color,lw,label):
plt.plot(x_axis, y_axis, color, lw=lw, label=label)
'''
畫柱狀圖
edgecolor柱狀圖的邊框顏色 align x軸的ticks對齊方式
color 柱狀圖的顏色 label 圖示標題
'''
def plotBarChart(x,y,color,edgecolor,label,align):
plt.bar(x, y, color, edgecolor, label, align)
'''
畫散點圖
s = size 點的大小 c = color alpha = 點的透明度
'''
def plotScatter(x,y,color,alpha):
plt.scatter(x, y, s=15, c=color, alpha = alpha)
'''
畫3D圖
cmp plt.cm.jet/ plt.get_cmap('rainbow')
'''
def plot3DChart(x,y,z):
# 設置三維座標
fig = plt.figure()
ax = Axes3D(fig)
X, Y = np.meshgrid(x, y) # XY平面的網格數據
# 畫3d圖
ax.plot_surface(X, Y, z, rstride=1, cstride=1, cmap=plt.cm.jet)
'''
顯示每個圖的y值
ha va 爲水平和垂直對齊方式
將x/y作爲一對(x,y)
'''
def setBarText(x,y):
data = zip(x,y)
for x,y in data:
plt.text(x+0.1,y+0.1,float(y),ha='center',va='bottom')
'''
設置legend 的位置參數
best
upper right
upper left
lower left
lower right
right
center left
center right
lower center
upper center
center
'''
def setLegend(loc):
plt.legend(loc=loc)
''''
設置X軸的範圍 plt.xlim(x.min()*1.1, x.max()*1.1)
'''
def setXLim(min,max):
plt.xlim(min, max)
'''
設置Y軸的範圍 plt.ylim(c.min()*1.1, s.max()*1.1)
'''
def setYLim(min,max):
plt.ylim(min, max)
#根據相應座標數 轉換成字符座標
'''
設置X座標的對應表示
'''
def setXTicks(x_data,x_label):
plt.xticks(x_data,x_label)
'''
設置Y座標的對應表示
'''
def setYTicks(y_data,y_label):
plt.yticks(y_data,y_label)
'''
設置座標軸的位置
'''
def setAxisPosition():
ax = plt.gca()
#先把右邊和上邊的邊界設置爲不可見
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#然後把下邊界和左邊界移動到0點
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))
'''
添加標註
'''
def setAnnotate(x0,y0,color):
plt.scatter(x0,y0,color=color) #顯示一個點
plt.plot([x0,x0],[0,y0],'b--') #在點到x軸畫出垂直線
#標註方法1
plt.annotate('y = sin(x)' % y0,xy=(x0,y0),xycoords='data',xytext=(+30,-30),textcoords='offset points',
arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=-0.2'))
'''
標註方法2
'''
def setText(x0,y0,text):
plt.text(x0+0.1, y0,text)
'''
設置座標軸字體的透明度
'''
def setTransparency():
ax=plt.gca()
for label in ax.get_xticklabels()+ax.get_yticklabels():
label.set_fontsize(12)
label.set_bbox(dict(facecolor='white',edgecolor='none',alpha=0.7))
'''
設置多個子圖區
plt.subplot(xyz)
x表示行
y表示列
z表示圖表序號位置
'''
def setSubPlot(posion):
plt.subplot(posion)
'''
顯示圖表
'''
def showChart():
plt.show()
使用matplotlib畫圖的簡單封裝
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.