在PyQt4中使用matplotlib
matplotlib作为Python中著名的数据可视化工具,其官网也提供了在PyQt4中使用的源码,这里举一个应用实例,以备不时之需。
1) 利用Qt Designer创建GUI界面
Demo的GUI界面,如图1所示,其中利用QFrame作为放置matplotlib界面的容器。然后调用pyuic4.bat -o ui_maindialog.py maindialog.ui编译UI界面。

图1 GUI设计界面
2) maindialog.py程序代码
#!/usr/bin/env python
#-*- coding: utf-8 -*- import numpy as np
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from ui_maindialog import Ui_MainDialog
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas # matplotlib对PyQt4的支持
from matplotlib.figure import Figure class MainDialog(QDialog, Ui_MainDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
self._createFigures()
self._createLayouts()
# 创建matplotlib的画布
def _createFigures(self):
self._fig = Figure(figsize=(8, 6), dpi=100, tight_layout=True)
self._fig.set_facecolor("#F5F5F5") # 背景色
self._fig.subplots_adjust(left=0.08, top=0.92, right=0.95, bottom=0.1) # Margins
self._canvas = FigureCanvas(self._fig) # 画布
self._ax = self._fig.add_subplot(111) # 增加subplot
self._ax.hold(True)
self._initializeFigure() def _createLayouts(self):
layout = QHBoxLayout(self.frame)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self._canvas) # Add Matplotli def _initializeFigure(self):
Font = {'family': 'Tahoma',
'weight': 'bold',
'size': 10}
# Abscissa
self._ax.set_xlim([380, 780])
self._ax.set_xticks([380, 460, 540, 620, 700, 780])
self._ax.set_xticklabels([380, 460, 540, 620, 700, 780], fontdict=Font)
self._ax.set_xlabel("Wavelength (nm)", fontdict=Font)
# Ordinate
self._ax.set_ylim([0.0, 1.0])
self._ax.set_yticks(np.arange(0.0, 1.1, 0.2))
self._ax.set_yticklabels(np.arange(0.0, 1.1, 0.2), fontdict=Font)
self._ax.set_ylabel("Spectral Radiance (W/(m$^2$*sr*nm))", fontdict=Font) self._ax.grid(True) # Grid On def _updateFigures(self):
Font = {'family': 'Tahoma',
'weight': 'bold',
'size': 10} self._ax.clear() maxY = 0.0 x = np.arange(380, 781)
y = np.random.rand(401) self._ax.plot(x, y, 'r', label="Data")
maxY = max(y)
if maxY <= 0:
self._initializeFigure()
else:
self._fig.subplots_adjust(left=0.11, top=0.92, right=0.95, bottom=0.1)
# Abscissa
self._ax.set_xlim([380, 780])
self._ax.set_xticks([380, 460, 540, 620, 700, 780])
self._ax.set_xticklabels([380, 460, 540, 620, 700, 780], fontdict=Font)
self._ax.set_xlabel("Wavelength (nm)", fontdict=Font)
# Ordinate
self._ax.set_ylim([0.0, maxY])
self._ax.set_yticks([0.0, maxY / 4.0, maxY / 2.0, maxY * 3 / 4.0, maxY])
self._ax.set_yticklabels(
["%.1e" % 0.0, "%.1e" % (maxY / 4.0), "%.1e" % (maxY / 2.0), "%.1e" % (maxY * 3.0 / 4.0),
"%.1e" % maxY], fontdict=Font)
self._ax.set_ylabel("Spectral Radiance (W/(m$^2$*sr*nm))", fontdict=Font) self._ax.grid(True)
self._ax.legend(loc="best", fontsize="small").draggable(state=True) # Legend
self._canvas.draw() @pyqtSlot()
def on_plotPushButton_clicked(self):
self._updateFigures()
初始界面如图2所示:

图2 GUI初始界面
3) 点击plot按键后
界面显示见图3:

图3 点击Plot按键后界面
在PyQt4中使用matplotlib的更多相关文章
- uwsgi+flask环境中安装matplotlib
uwsgi+flask的python有自身的virtual environment,可以通过如下命令进入 . venv/bin/activate 虽然通过sudo apt-get install py ...
- python中利用matplotlib绘图可视化知识归纳
python中利用matplotlib绘图可视化知识归纳: (1)matplotlib图标正常显示中文 import matplotlib.pyplot as plt plt.rcParams['fo ...
- 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令
使用python中的matplotlib 画图,show后关闭窗口,继续运行命令 在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0 ...
- python 1: 解决linux系统下python中的matplotlib模块内的pyplot输出图片不能显示中文的问题
问题: 我在ubuntu14.04下用python中的matplotlib模块内的pyplot输出图片不能显示中文,怎么解决呢? 解决: 1.指定默认编码为UTF-8: 在python代码开头加入如下 ...
- 【Python开发】使用python中的matplotlib进行绘图分析数据
matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备, ...
- pyqt中使用matplotlib绘制动态曲线
一.项目背景: 看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个. 二.需求描述: 1)X轴显示时间点,显示长度为1分钟 ...
- pyqt中使用matplotlib绘制动态曲线 – pythonic
一.项目背景: 看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个. 二.需求描述: 1)X轴显示时间点,显示长度为1分钟 ...
- Python 中使用 matplotlib 绘图中文字符显示异常的问题
最近在使用 Python matplotlib 绘制图表时发现中文字符不能正确显示:比如在绘制折线图时,中文全部显示成▢▢▢的格式,虽然将数据改成英文就没什么问题,但是所有数据都这么做时不可行的,于是 ...
- 【转】 Python 中,matplotlib绘图无法显示中文的问题
在python中,默认情况下是无法显示中文的,如下代码: import matplotlib.pyplot as plt # 定义文本框和箭头格式 decisionNode = dict(boxsty ...
随机推荐
- WCF—Binding
原文地址:http://www.cnblogs.com/jams742003/archive/2010/01/13/1646379.html Binding描述了哪些层面的信息 一个Binding包含 ...
- bzoj 1191 [HNOI2006]超级英雄Hero(最大基数匹配)
1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2813 Solved: 1331[Submit][ ...
- 使用Array
public class UsingArray { public static void output(int[]Array) { if(Array!=null) ...
- multipath.conf
# This is a basic configuration file with some examples, for device mapper# multipath.# For a comple ...
- logback logback.xml 常用配置详解
一:根节点<configuration>包含的属性: scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配置文 ...
- 第七讲:HTML5中的canvas两个小球全然弹性碰撞
<html> <head> <title>小球之间的碰撞(全然弹性碰撞)</title> <script src="../js/jsce ...
- Android WindowManager的使用
经常,我们看到在桌面上可移动的悬浮窗,这种场景还是很多的, 像流量统计,桌面歌词等,安全软件的清理小部件 这种小部件主要是通过 WindowManager ; WindowManager.Layout ...
- Android音频开发之——如何播放一帧音频
本文重点关注如何在Android平台上播放一帧音频数据.阅读本文之前,建议先读一下<Android音频开发(1):基础知识>,因为音频开发过程中,经常要涉及到这些基础知识,掌握了这些重要的 ...
- codevs 1515 跳
/* 画矩阵找规律发现是杨辉三角 Cg (i,j)= C (i+j,i); 贪心走的话 沿着0行(列)一直走然后拐直角 拐弯后每个格子都累加 Cg (n,0) + Cg (n,1) + Cg (n,2 ...
- Oracle中的over(partition by...)分析函数及开窗函数
假设有一张表student Name Score InsertTime (Name:姓名 Score:成绩 InsertTime:考试时间) 张三 20 2015-08-08 ...