欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python之Matplotlib.pyplot

程序员文章站 2022-07-13 21:51:13
...
最近将windows系统删除了,没有matlab,所以考虑使用python在mac os直接实现数据处理及画图功能。主要使用numpy和matplotlib来实现matlab的功能,numpy可以进行矩阵的处理,而matplotlib.pyplot用来实现图形化功能。

matplotlib参考文档地址

安装方式:
python3.6 -mpip install -U pip
python3.6 -mpip install -U matplotlib
python3.6 -mpip install -U numpy
由于采用homebrew安装python3.6.3的版本link为了python3.6版本。理解上集成了pip的python版本才能这样使用?

顺带提提numpy的用途
测试代码
import numpy as np

#np.array 和 list差别
pyValueA = [1,2,3,4]
pyValueB = [6,7,8,9]
addPyValueAB = pyValueA+pyValueB
print("python list: ",addPyValueAB)

npValA = np.array(pyValueA)
npValB = np.array(pyValueB)
addnpValueAB = npValA+npValB
print("numpy array add: ",addnpValueAB)

pyValAM2 = pyValueA * 2
print("python list multiple:",pyValAM2)
npValAM2 = npValA * 2
print("numppy multiple:",npValAM2)

#另外numpy可以进行矩阵运算
npMatrixA = np.matrix(pyValueA)
npMatrixAT = np.transpose(npMatrixA) #转置
npTmp = npMatrixA * npMatrixAT
print("numpy matrix src:",npMatrixA,"multiple rslt:",npTmp)
运行结果:
python list: [1, 2, 3, 4, 6, 7, 8, 9]
numpy array add: [ 7 9 11 13]
python list multiple: [1, 2, 3, 4, 1, 2, 3, 4]
numppy multiple: [2 4 6 8]
numpy matrix src: [[1 2 3 4]] multiple rslt: [[30]]
同时numpy整合了mean、min、max、std、var等类似matlab函数的功能。
可以看到list的加/乘都是对元素个数的扩展,而np.array则是对元素进行运算。

matplotlib.pyplot的图形接口函数
pyplot中提供了类似matlab的易用接口函数,看看简单常用的画图接口。
#创建图形窗口:figure 同时可以设置图片大小、分辨率等
#点和线:plot
#条形图:bar
#箱图:box/boxplot[
#柱状图:hist/hist2d
#杆图:stem
#饼图:pie
#坐标限制:axis xlim/ylim
#图形说明:legend
#坐标说明:xlabel ylable
#标题说明:title/suptitle
#显示图形:show
#保存图片:savefig
#网格线: grid
#图片分割:subplot/subplots_adjust(用于调整图片之间的间隔.)
#hold的问题:当期望多次画图结果在一个图形上显示时,需要使用到hold功能,说明上新版本不推荐使用hold函数,默认为hold=True的情况,如果需要清除之前的状态可以使用cla[clear current axes]和clf[clear the current figure]函数。

颜色的参数设置
#========== ========
#character color
#========== ========
#'b' blue
#'g' green
#'r' red
#'c' cyan
#'m' magenta
#'y' yellow
#'k' black
#'w' white
#========== ========

点和线类型设置
#================ ===============================
#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
#``'>'`` triangle_right marker
#``'1'`` tri_down marker
#``'2'`` tri_up marker
#``'3'`` tri_left marker
#``'4'`` tri_right marker
#``'s'`` square marker
#``'p'`` pentagon marker
#``'*'`` star marker
#``'h'`` hexagon1 marker
#``'H'`` hexagon2 marker
#``'+'`` plus marker
#``'x'`` x marker
#``'D'`` diamond marker
#``'d'`` thin_diamond marker
#``'|'`` vline marker
#``'_'`` hline marker
#================ ===============================

尝试pyplot的测试代码
import matplotlib.pyplot as plt

a = [1,2,3,4]
b = [5,6,7,8]

fig = plt.figure(1)
plt.suptitle("point test")
plt.title("point test1")
plt.title("point test2")

#点的类型
plt.plot(a,b,'bo')
#plt.cla() 
#plt.clf()
c = [7,8,9,10]
plt.plot(a,c,'r-')
plt.legend(['a and b','a and c'])
plt.xlabel('a value')
plt.ylabel('c value')
plt.axis([0,10,0,10])
plt.xlim([1,9])
plt.grid(True)
fig.show() #display a figure
fig.savefig("pointTest1.png")
plt.close()

#子图的画法
fig = plt.figure(2)
fig = plt.subplot(2,1,1)
plt.bar(a,a)
plt.title('bar test')
plt.suptitle('bartest')

fig = plt.subplot(2,1,2)
plt.subplots_adjust(hspace = 0.5)
e = [1,2,7,10,12,16,18,30]
plt.boxplot(e)
plt.suptitle('stem test')
plt.grid(True)
plt.title('stem test')
plt.savefig('test2.png')

得到结果
pointTest1.png
python之Matplotlib.pyplot
test2.png
python之Matplotlib.pyplot
关于subplots_adjust的用途
在测试程序没有使用plt.subplots_adjust(hspace = 0.5)时,test2.png的图形会出现重叠。
python之Matplotlib.pyplot
如何查看缺省参数的值
在使用help查看函数使用方法时,经常看到缺省参数,如figure函数中的figsize,dpi等。在使用help(pyplot.figure)查看帮助时并没有明确给出相应的值,怎么得到具体值呢?方法如下:
import matplotlib
matplotlib.rcParams['figure.figsize']
matplotlib.rcParams['figure.dpi']

相关标签: python pyplot