From: Sourabh B. <sb...@gm...> - 2012-03-12 20:30:17
|
I am getting a segmentation fault when I try to declare a new image name. I can replace a existing image correctly. Why am I getting the error at the getSaveFileName dialog ?? Can anyone help me with the code. import sys from PyQt4 import QtGui, QtCore, Qt import numpy as np from matplotlib.figure import Figure import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar def randrange(n, vmin, vmax): return (vmax-vmin)*np.random.rand(n) + vmin class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.main_frame = QtGui.QWidget() self.dpi=100 self.i=1 self.fig = Figure((5.0, 5.0), dpi=self.dpi) self.scatterpts = [] self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.ax = self.fig.gca(projection='3d') self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) self.btn = QtGui.QPushButton('Button', self) self.btn.resize(self.btn.sizeHint()) self.btn.clicked.connect(self.drawplot) self.btn1 = QtGui.QPushButton('Save', self) self.btn1.resize(self.btn.sizeHint()) self.btn1.clicked.connect(self.saveplot) self.setWindowTitle('Tooltips') Box =QtGui.QVBoxLayout() Box.addWidget(self.canvas) Box.addWidget(self.btn) Box.addWidget(self.btn1) Box.addWidget(self.mpl_toolbar) self.main_frame.setLayout(Box) self.main_frame.show() def drawplot(self): if self.i==0: for pt in self.scatterpts : pt.remove() self.scatterpts = [] n = 500 for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]: xs = randrange(n, 23, 32) ys = randrange(n, 0, 100) zs = randrange(n, zl, zh) pt = self.ax.scatter(xs, ys, zs, c=c, marker=m, s=40) self.scatterpts.append(pt) self.ax.set_xlabel('X Label') self.ax.set_ylabel('Y Label') self.ax.set_zlabel('Z Label') self.canvas.draw() self.canvas.print_figure('trial.png', dpi=self.dpi) self.i=0 def saveplot(self): fname = QtGui.QFileDialog.getSaveFileName(self, 'Save file', '/home/untitled.png', 'Images (*.png *.xpm *.jpg)') self.canvas.print_figure(fname, dpi=100) def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main() |