使用matplotlib画图的简单封装

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()

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值