Python入门图文教程,几行代码就能搞定

上一篇 / 下一篇  2020-09-18 11:22:25

  什么是Matplotlib?
  Matplotlib是一个Python 2D绘图库,它以各种硬拷贝格式和跨平台的交互环境生成出版质量图形。Matplotlib可以用于Python脚本、Python和IPython shell、Jupyter笔记本、Web应用程序服务器和四个图形用户界面工具包。
  Matplotlib试图让事情变得简单,让复杂的事情变得可能。您可以生成绘图,直方图,功率谱,柱状图,错误图,散点图等,只需几行代码。
  为了进行简单的绘图,pyplot模块提供了一个类似matlab的接口,特别是在与IPython结合使用时。对于高级用户,您可以通过一个面向对象的界面或一组MATLAB用户熟悉的函数来完全控制线条样式、字体属性、轴属性等。
  安装
  您可以通过使用pip install matplotlib来简单安装matplotlib。
  什么是PyQt5 ?
  Qt是一组跨平台的C++库,这些库实现了用于访问现代桌面和移动系统的许多方面的高级api。这些包括位置和定位服务,多媒体,NFC和蓝牙连接,一个基于铬的web浏览器,以及传统的UI开发。
  PyQt5是针对Qt v5的一组全面的Python绑定。它被实现为35个以上的扩展模块,使Python可以在包括iOS和Android在内的所有支持平台上作为C++的替代应用开发语言。
  PyQt5还可以嵌入到基于C++的应用程序中,以允许这些应用程序的用户配置或增强这些应用程序的功能。
  安装
  GPL版本的PyQt5可以从PyPI安装:
  pip install PyQt5 
  包括Qt的LGPL版本所需部件的副本。
  pip还将从sdist包构建和安装绑定,但Qt的qmake工具必须在PATH上。
  sip安装工具还将安装来自sdist包的绑定,但允许您配置安装的许多方面。
  现在,这是如何在PyQt5中嵌入Matplotlib生成图像的完整代码。
  from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton  
  import sys  
  from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas  
  from matplotlib.figure import Figure  
  import numpy as np  
  class Window(QMainWindow):  
      def __init__(self):  
          super().__init__()  
          title = "在PyQt5中嵌入Matplotlib - www.linuxmi.com"  
          top = 400  
          left = 400  
          width = 1000  
          height = 600  
          self.setWindowTitle(title)  
          self.setGeometry(top, left, width, height)  
          self.MyUI()  
      def MyUI(self):  
          canvas = Canvas(self, width=8, height=4)  
          canvas.move(0,0)  
          button = QPushButton("点击我", self)  
          button.move(100, 500)  
          button2 = QPushButton("再次点击我", self)  
          button2.move(350, 500)  
  class Canvas(FigureCanvas):  
      def __init__(self, parent = None, width = 5, height = 5, dpi = 100):  
          fig = Figure(figsize=(width, height), dpidpi=dpi)  
          self.axes = fig.add_subplot(111)  
          FigureCanvas.__init__(self, fig)  
          self.setParent(parent)  
          self.plot()  
      def plot(self):  
          x = np.array([50,30,40,20])  
          labels = ["LinuxMi.com", "Debian", "Linux", "Python"]  
          ax = self.figure.add_subplot(111)  
          ax.pie(x, labelslabels=labels)  
  app = QApplication(sys.argv)  
  window = Window()  
  window.show()  
  app.exec() 
  我们导入了所需的库,基本上是我们需要的
  from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton  
  import sys  
  from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas  
  from matplotlib.figure import Figure  
  import numpy as np 
  这是我们的主窗口类它继承自QMainWindow,我们对窗口有一些要求,比如窗口的标题,宽度,高度,我们还在这个类中调用了MyUI()方法。
  class Window(QMainWindow):  
      def __init__(self):  
          super().__init__()  
          title = "在PyQt5中嵌入Matplotlib - www.linuxmi.com"  
          top = 400 
          left = 400  
          width = 1000  
          height = 600  
          self.setWindowTitle(title)  
          self.setGeometry(top, left, width, height)  
          self.MyUI() 
  在这个方法中,我们创建了一个带有两个QPushButton的Canvas。 
  def MyUI(self):  
          canvas = Canvas(self, width=8, height=4)  
          canvas.move(0,0)  
          button = QPushButton("点击我", self)  
          button.move(100, 500)  
          button2 = QPushButton("再次点击我", self)  
          button2.move(350, 500) 
  和这是我们的Canvas类,继承自FigureCanvas。
  class Canvas(FigureCanvas):  
      def __init__(self, parent = None, width = 5, height = 5, dpi = 100):  
          fig = Figure(figsize=(width, height), dpidpi=dpi)  
          self.axes = fig.add_subplot(111)  
          FigureCanvas.__init__(self, fig)  
          self.setParent(parent)  
          self.plot() 
  在这里,我们还将在PyQt5窗口中绘制一个饼状图。
  def plot(self):  
        x = np.array([50,30,40,20])  
        labels = ["LinuxMi.com", "Debian", "Linux", "Python"] 
        ax = self.figure.add_subplot(111)  
        ax.pie(x, labelslabels=labels) 
  因此在这里,每个PyQt5应用程序都必须创建一个应用程序对象。sys.argv参数是命令行的参数列表。
  app = QApplication(sys.argv) 
  最后,我们进入应用程序的主循环。事件处理从这里开始。
  mainloop从窗口系统接收事件并将它们分派给应用程序小部件。
  app.exec()  
  sys.exit() 
  运行完整的代码,结果如下:


TAG: 软件开发 Python

 

评分:0

我来说两句

Open Toolbar