Matplotlib是一个Python 2D绘图库, 只需几行代码即可生成绘图,直方图,功率谱,条形图,错误图,散点图等。 有关示例,请参阅示例图和缩
import matplotlib.pyplot as plt import numpy as np class TestPlot(object): def __init__(self,plt): self.plt = plt #定义内部属性 # 解决中文乱码问题(第二种) plt.rcParams['font.sans-serif'] = ['SimHei'] #指定编码 plt.rcParams['axes.unicode_minus'] = False #定义面积图方法(*********************************************) def my_area(self): #定义日期区间 data = ['2019-03-01','2019-03-02','2019-03-03','2019-03-04','2019-03-05'] #定义数据 #收入 earn = [166,155,355,422,622] #支出 pay = [[16,30,25,46,20],[10,15,20,144,122]] #将数据传入方法 self.plt.stackplot(data,earn,pay,colors=['green','yellow','orange']) #生成图例 self.plt.plot([],[],color='green',label="收入") self.plt.plot([],[],color='yellow',label="午餐") self.plt.plot([],[],color='orange',label="晚餐") #设置标题 self.plt.title("面积图样例") self.plt.legend() self.plt.show() #定义柱状图(*********************************************) def my_bar(self): my_plt = self.plt GDP = [12404.1,13396.222,5335.22,5223.22] my_plt.bar(["北京",'上海','深圳','重庆'],GDP,align='center',color="lime",alpha=0.8) my_plt.ylabel("生产总值") #添加标题 my_plt.title("直辖市GDP") #刻度范围 my_plt.ylim([5000,15000]) my_plt.show() #定义饼图(*********************************************) def my_pie(self): my_plt = self.plt beijing = [17,22,23,46] #定义标签 label = ['2-3年','3-4年','4-5年','五年以上'] color = ['red','green','blue','purple'] #将数值最大的突出展示 indic = [] #使用enumerate方法来添加索引 for index,i in enumerate(beijing): if i == max(beijing): indic.append(0.5) elif index == 1: indic.append(0.2) else: indic.append(0) # for i in beijing: # if i == max(beijing): # indic.append(0.1) # else: # indic.append(0) #将数据传入 ,数据,标签,颜色,角度,阴影,突出显示 my_plt.pie( beijing, labels= label, colors = color, startangle=90, shadow = True, explode = tuple(indic), #格式化数字 autopct = "%1.1f%%" ) # 设置标题 my_plt.title("饼图实例-统计北京程序员工龄占比") my_plt.show() ##曲线图走势图(*********************************************) def my_line(self): my_line= self.plt # #设置本机字体(第一种) # font = FontProperties(fname='C:/Windows/Fonts/simhei.ttf',size=15) #定制数据 x1 = ['2019-03-01','2019-03-02','2019-03-03','2019-03-04','2019-03-05','2019-03-06'] y1 = [0,6,9,5,3,2] y2 = [10,12,16,12,16,17] #填充数据 my_line.plot(x1,y1,label='temperature') my_line.plot(x1,y2,label='water') #设置标题 # my_line.title("趋势图",FontProperties=font) my_line.title("趋势图") # #显示图例 my_line.legend() my_line.show() #点状图(*********************************************) def my_dot(self): my_dot = self.plt x = list(range(101)) y = [xvalue * np.random.rand() for xvalue in x] #填充数据 s 点的大小和粗细 c 颜色 my_dot.scatter(x,y,s=20,c='lime') #绘图 my_dot.show() # 定义横向条形图(*********************************************) def my_barh(self): my_plt = self.plt #定义价格 price = [40,32.8,20,19.6] #将数据传入 my_plt.barh(range(4),price,align="center",color="red",alpha=0.5) #设置标签 my_plt.xlabel('价格') #将数据传入y轴 my_plt.yticks(range(4),['红楼梦','三国演义','西游记','水浒传']) #设置上下限 my_plt.xlim([10,60]) #设置标题 my_plt.title("四大名著") #绘制 my_plt.show() if __name__ == "__main__": testplot = TestPlot(plt) # testplot.my_area() # testplot.my_bar() # testplot.my_pie() # testplot.my_line() # testplot.my_dot() # testplot.my_barh() # # Matplot 生成子图(一个图中两个子图) # #第一组数据 # x1 = list(range(5)) # y1 = list(map(lambda x:x**2,x1)) # #第二组数据 # x2 = list(range(4,10)) # y2 = list(map(lambda x:x**2/2,x2)) # ##做第一幅图 2*1矩阵 # ax1 = plt.subplot(121) # #传入数据 # ax1.plot(x1,y1) # ax2 = plt.subplot(122) # ax2.plot(x2,y2) # # #删除字图 # # plt.delaxes(ax2) # # #增加字图 # # plt.subplot(ax2) # plt.show() #画散点图 # df = pd.read_excel('tips.xlsx','sheet1') # df.plot(kind='hist',x='tip',y='total_bill',color='lime',label='bill_tip') # plt.title("heelo") # plt.show()
略图库。