python 的 matplotlib

来源:互联网 发布:java教育 编辑:程序博客网 时间:2024/06/03 17:28

本文会一直更新matplot的画图技巧,使用方法

matplotlib教程

matplotlib API

1、实例一

本实例展示了matplotlib画图的基本流程

import matplotlib.pyplot as plt#  step 1 prepare datax = [1,2,3,4]y = [10,20,25,30]#  step 2 create plotfig = plt.figure()# step 3 plot settingsax = fig.add_subplot(2,2,1)ax.plot(x, y, color='blue', linewidth=2)ax.set_xlim(1,6.5)bx = fig.add_subplot(2,1,2)bx.scatter([2,4,6,5],           [3,7,20,4],           color = 'green',           marker= '*')# step 4 plot imshowplt.show()# if you want to saveplt.savefig('foo.png')

结果

这里写图片描述

2、实例二

# -*- coding: utf-8 -*-import numpy as npimport matplotlib .pyplot as pltfrom scipy import interpolatefrom scipy import optimizex = [0.01, 0.05, 0.08 , 0.1 ,0.2,0.4, 0.8, 1, 1.24,1.53,2]y = [97.723, 97.456, 98.346, 99.124, 99.234, 98.5,98.43, 97.71,97.4, 97.24,97.20]x1 = [0.01,  0.08 , 0.1 ,0.2,0.4, 1, 1.24,1.53,2]y1 = [97.723, 98.346, 99.124, 99.234, 98.5, 97.71,97.4, 97.24,97.20]t1 = [0.05, 97.456]t2 = [0.8, 98.43]fig = plt.figure()img = fig.add_subplot(111)img.plot(x,y,color="blue",linewidth=1)img.plot(x1,y1,'k^')xnew = np.linspace(min(x),max(x),300)f2 = interpolate.interp1d(x,y,kind=2)plt.plot(xnew, f2(xnew),'-.',color="green")img.set_title("the sensitiveness of hyper parameter $\lambda$",size=14)#r'$\lambda$'img.set_xlabel('$\lambda$' + '- type:float',size=12)img.set_ylabel('Verfication on LFW Accuracy(%)',size=12)img.set_ylim(96,100)plt.grid(True)plt.scatter([t1[0],],[t1[1],], 30, color='red')plt.scatter([t2[0],],[t2[1],], 30, color='red')plt.annotate(r'A(0.05, 97.456)',             t1, xycoords='data',             xytext=(-15,-30), textcoords='offset points', fontsize=12,             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))plt.annotate(r'B(0.8, 98.43)',             t2, xycoords='data',             xytext=(+40,+40), textcoords='offset points', fontsize=12,             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))plt.legend(['Linear','data','cubic smooth','key point'],loc='lower right',ncol=2)plt.show()

结果


这里写图片描述

3、一些查表的参数

3.1.1 颜色& 线形

1、颜色

character color ‘b’ blue ‘g’ green ‘r’ red ‘c’ cyan ‘m’ magenta ‘y’ yellow ‘k’ black ‘w’ white

2、线形

character description ‘-‘ solid line style ‘–’ dashed line style ‘-.’ dash-dot line style ‘:’ dotted line style ‘.’ point marker ‘,’ pixel marker ‘o’ circle marker ‘v’ triangle_down marker ‘^’ triangle_up marker ‘<’ triangle_left marker ’s’ square marker ‘p’ pentagon marker ‘x’ x marker ‘D’ diamond marker ‘_’ hline marker

tip-颜色和线形可随意搭配。 ‘b^’ 或‘g>’或 ”bo”

3.1.2 x 轴显示

rotation=30, 可以用于x轴的标注

3.2

小写希腊字母

原创粉丝点击