Matplotlib-a case of basemap and pyqt4

Source: Internet
Author: User


Some netizens sent messages and asked questions:

I want to build a small software. I can draw contour lines on the software (the contour is on the map) and use basemap. I need to embed matplotlib basemap into the pyqt canvas and check a lot of information, it can embed simple XY coordinates of pyqt into pyqt, but cannot embed basemap. I wonder if you can give me some advice?

It's really embarrassing. Although I have read a little bit about pyqt4 and matplotlib, I don't even know what basemap is.

If you cannot decide, search for Google...

Basemap

Put simply: matplotlib provides a tool for drawing 2D data on a map.

  • The matplotlib basemap toolkit is a library for plow.2d data on maps in Python. it is similar in functionality to the MATLAB mapping toolbox, The IDL mapping facilities, grads, or the generic mapping tools. pyngl and cdat are other libraries that provide similar capabilities in Python.
Install

It is not part of the default installation of matplotlib (the source code is more than MB, so it is no wonder that it is not included by default ). Therefore, we need to install it by ourselves:

Source code repository:

https://github.com/matplotlib/basemap

Use git or directly download the compressed source code package. Decompress the package and check readme.

I used Ubuntu 11.04 to install something first.

sudo apt-get install python-matplotlib swig python2.7-dev

Then follow the installation instructions:

  • Go to the geos subdirectory
./configure --enable-pythonmakesudo make install
  • Back to Top-level directory
sudo python setup.py install
Running example

Follow readme to run the example to check whether the installation is successful. Switch to the examples directory and run simpletest. py.

python simpletest.py
Embedded in pyqt4
  • In the basemap example, the example of wxpython is embedding_map_in_wx.py.
  • Let's refer to it to write a pyqt4 example. Run it first.

 

The Code is as follows:

"""An example of how to use Basemap in pyqt4 application.Copyright(C) 2011 dbzhang800#gmail.com"""from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvasfrom matplotlib.figure import Figurefrom mpl_toolkits.basemap import Basemapfrom PyQt4 import QtGuiclass Widget(FigureCanvas):    def __init__(self, parent=None, width=5, height=4, dpi=100):        fig = Figure(figsize=(width, height), dpi=100)        FigureCanvas.__init__(self, fig)        self.setParent(parent)        self.axes = fig.add_subplot(111)        map = Basemap(ax=self.axes)        map.drawcoastlines()        map.drawcountries()        map.drawmapboundary()        map.fillcontinents(color='coral', lake_color='aqua')        map.drawmapboundary(fill_color='aqua')        self.setWindowTitle("PyQt4 and Basemap -- dbzhang800")if __name__ == "__main__":    import sys    app = QtGui.QApplication(sys.argv)    w = Widget()    w.show()    sys.exit(app.exec_())
Reference
  • Http://matplotlib.sourceforge.net/basemap/doc/html/index.html

  • Http://matplotlib.sourceforge.net/basemap/doc/html/api/basemap_api.html

  • Http://www.scipy.org/Cookbook/Matplotlib/Maps

  • Http://matplotlib.sourceforge.net/examples/user_interfaces/embedding_in_qt4.html

  • Http://hi.baidu.com/cyclone/blog/item/527dd21bbd73e7d8ad6e759c.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.