在此我只想强调一下各种库的文档、示例等自带资料的重要性。
下面都是matplotlib的demo里面的,正是你需要的这些功能。
multiple_figs_demo.py
#!/usr/bin/env python# Working with multiple figure windows and subplots
from pylab import *
t = arange(0.0, 2.0, 0.01)
s1 = sin(2*pi*t)
s2 = sin(4*pi*t)
figure(1)
subplot(211)
plot(t,s1)
subplot(212)
plot(t,2*s1)
figure(2)
plot(t,s2)
# now switch back to figure 1 and make some changes
figure(1)
subplot(211)
plot(t,s2, 'gs')
setp(gca(), 'xticklabels', [])
figure(1)
savefig('fig1')
figure(2)
savefig('fig2')
show()
下面是关于pdf的multipage_pdf.py
# This is a demo of creating a pdf file with several pages.import datetime
import numpy as np
import matplotlib
from matplotlib.backends.backend_pdf import PdfPages
from pylab import *
# Create the PdfPages object to which we will save the pages:
pdf = PdfPages('multipage_pdf.pdf')
figure(figsize=(3,3))
plot(range(7), [3,1,4,1,5,9,2], 'r-o')
title('Page One')
savefig(pdf, format='pdf') # note the format='pdf' argument!
close()
rc('text', usetex=True)
figure(figsize=(8,6))
x = np.arange(0,5,0.1)
plot(x, np.sin(x), 'b-')
title('Page Two')
pdf.savefig() # here's another way - or you could do pdf.savefig(1)
close()
rc('text', usetex=False)
fig=figure(figsize=(4,5))
plot(x, x*x, 'ko')
title('Page Three')
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
close()
# We can also set the file's metadata via the PdfPages object:
d = pdf.infodict()
d['Title'] = 'Multipage PDF Example'
d['Author'] = u'Jouni K. Sepp\xe4nen'
d['Subject'] = 'How to create a multipage pdf file and set its metadata'
d['Keywords'] = 'PdfPages multipage keywords author title subject'
d['CreationDate'] = datetime.datetime(2009,11,13)
d['ModDate'] = datetime.datetime.today()
# Remember to close the object - otherwise the file will not be usable
pdf.close()
PyQt是一个基于Qt的接口包,可以直接拖拽控件设计UI界面,下面我简单介绍一下这个包的安装和使用,感兴趣的朋友可以自己尝试一下:1、首先,安装PyQt模块,这个直接在cmd窗口输入命令“pip install pyqt5”就行,如下,整个模块比较大,下载过程需要等待一会儿,保持联网:
2、安装完成后,我们就可以直接打开Qt自带的Qt Designer设计师设计界面了,这里默认会安装到site-packages->PyQt5->Qt->bin目录,打开后的界面如下,可以直接新建对话框等窗口,所有的控件都可以直接拖拽,编辑属性,非常方便:
相关推荐:《Python基础教程》
3、这里我简单的设计了一个登录窗口,2个输入框和2个按钮,如下,这里可以直接使用QSS对界面进行美化(设置styleSheet属性即可),类似网页的CSS,如果你有一定的前端基础,那么美化起来会非常容易:
设计完成后,还只是一个ui文件,不是现成的Python代码,还需要借助pyuic5工具(也在bin目录下)才能将ui文件转化为Python代码,切换到ui文件所在目录,输入命令“pyuic5 -o login.py login.ui”即可(这里替换成你的ui文件),转化成功后的Python代码如下(部分截图):
还需要在最下面添加一个main函数,创建上面Ui_Form类对象显示窗口即可,如下:
最后点击运行程序,效果如下,和刚才设计的界面效果一模一样:
至此,我们就完成了利用Python的PyQt模块直接拖拽控件来设计UI界面。总的来说,整个过程非常简单,只要你有一定的Python基础,熟悉一下操作过程,很快就能掌握的,当然,还有许多其他UI开发模块,像tkinter,wxPython,Eric6等,也都非常不错。
在matplotlib下,一个Figure对象可以包含多个子图(Axes),可以使用subplot()快速绘制,其调用形式如下:[python] view plain copy
subplot(numRows, numCols, plotNum)
图表的整个绘图区域被分成numRows行和numCols列,plotNum参数指定创建的Axes对象所在的区域,如何理解呢?如果numRows = 3,numCols = 2,那整个绘制图表样式为3X2的图片区域,用坐标表示为(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)。这时,当plotNum = 1时,表示的坐标为(1,3),即第一行第一列的子图;看代码吧!