今天介紹一些Python中用於畫圖表圖形的類庫。由於有些時候,他們跟matlab同樣簡單,方便。並且有些也具備非凡的表現力。php
matplotlib,官網:http://matplotlib.sourceforge.net/,Matplotlib 是一個由 John Hunter 等開發的,用以繪製二維圖形的 Python 模塊。它利用了 Python 下的數值計算模塊 Numeric 及 Numarray,克隆了許多 Matlab 中的函數, 用以幫助用戶輕鬆地得到高質量的二維圖形。Matplotlib 能夠繪製多種形式的圖形包括普通的線圖,直方圖,餅圖,散點圖以及偏差線圖等;能夠比較方便的定製圖形的各類屬性好比圖線的類型,顏色,粗細,字體的大小 等;它可以很好地支持一部分 TeX 排版命令,能夠比較美觀地顯示圖形中的數學公式。我的比較推薦這個類庫。查看例子。html
Cairoplot,官網:http://linil.wordpress.com/2008/09/16/cairoplot-11/,(友情提示:須要FanQiang)。Cairoplot在網頁上的表現力堪比flex中的圖表圖形效果。可是這個彷佛只能跑在linux平臺上。因此不少windows用戶估計要失望了。python
Chaco, 官網:http://code.enthought.com/chaco/,Chaco是一個2D的繪圖庫。其中文簡單教程參考:http://hyry.dip.jp/pydoc/chaco_intro.htmllinux
Python Google Chart,官網:http://pygooglechart.slowchop.com/。從命名方式來看,這個確定與google chart扯上了關係。因此該類庫是對Google chart API的一個完整封裝。c++
PyCha,官網:https://bitbucket.org/lgs/pycha/wiki/Home。PyCha但是說是Cairo 類庫的一個簡單封裝,爲了是實現輕量級,以及容易使用,固然還作了一些優化等。git
pyOFC2,官網:http://btbytes.github.com/pyofc2/。它是Open Falsh Library的Python類庫。因此圖形具備Flash效果,能夠隨鼠標移動動態顯示圖標中信息,這是優越於其餘靜態圖示的。github
Pychart,官網:http://home.gna.org/pychart/。pyChart是用於建立高品質封裝的PostScript,PDF格式,PNG,或SVG圖表Python庫。segmentfault
PLPlot,官網:http://plplot.sourceforge.net/。PLPlot是用於建立科學圖表的跨平臺軟件包。以C類庫爲核心,支持各類語言綁定(C, C++, Fortran, Java, Python, Perl etc.)。開源免費。windows
reportlab,官網:http://www.reportlab.com/software/opensource/。這個咱們以前介紹過,參考http://www.codecho.com/installation-and-example-of-reportlab-in-python/。這個類庫支持在pdf中畫圖表。wordpress
Vpython,官網:http://www.vpython.org/index.html,VPython是Visual Python的簡寫,Visual是由Carnegie Mellon University(卡耐基-梅隆大學)在校學生David Scherer於2000年撰寫的一個Python 3D繪圖模塊。
Gallery
http://bokeh.pydata.org/gallery.html https://github.com/ContinuumIO/Bokeh/tree/master/examples
------------------------
吐槽一下:
pyCha 圖標過於簡單,並且顏色過於單一,更重要的是,還須要Cairo的支持,但是如今可以慶用的Cario很少。
Python Goolge Chart是幾個代碼中最簡單的,圖標無數據。只管美觀,不實用。
matplotlib,太過複雜,光光可用的模塊,就不下40-50,看得真暈。
最後,測試ChartDirector,最好。但是商業軟件,生成的圖像還有一個黃Banner。悲催呀~
----------------------------------
下面舉個 ChartDirector 的例子(注意 ChartDirector 包含 python、php、perl、c++等各類版本,這裏用python示範):
#!/usr/bin/python from pychartdir import * # The data for the line chart data0 = [60.2, 51.7, 81.3, 48.6, 56.2, 68.9, 52.8,60.2, 51.7, 81.3, 48.6, 56.2, 68.9] data1 = [30.0, 32.7, 33.9, 29.5, 32.2, 28.4, 29.8,30.0, 32.7, 33.9, 29.5, 32.2, 28.4, 29.8] labels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat","Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] # Create a XYChart object of size 300 x 180 pixels, with a pale yellow (0xffffc0) # background, a black border, and 1 pixel 3D border effect. c = XYChart(1000, 800, 0xffffc0, 0x000000, 1) # Set the plotarea at (45, 35) and of size 240 x 120 pixels, with white background. # Turn on both horizontal and vertical grid lines with light grey color (0xc0c0c0) c.setPlotArea(45, 35, 900, 700, 0xffffff, -1, -1, 0xc0c0c0, -1) # Add a legend box at (45, 12) (top of the chart) using horizontal layout and 8 pts # Arial font Set the background and border color to Transparent. c.addLegend(10, 742, 0, "", 18).setBackground(Transparent) # Add a title to the chart using 9 pts Arial Bold/white font. Use a 1 x 2 bitmap # pattern as the background. c.addTitle("Server Load (Jun 01 - Jun 07)", "arialbd.ttf", 9, 0xffffff ).setBackground(c.patternColor([0x004000, 0x008000], 2)) # Set the y axis label format to nn% # c.yAxis().setLabelFormat("{value}%") c.yAxis().setLabelFormat("{value}") # Set the labels on the x axis c.xAxis().setLabels(labels) # Add a line layer to the chart layer = c.addLineLayer() # Add the first line. Plot the points with a 7 pixel square symbol layer.addDataSet(data0, 0xcf4040, "Peak").setDataSymbol(SquareSymbol, 7) # Add the second line. Plot the points with a 9 pixel dismond symbol layer.addDataSet(data1, 0x40cf40, "Average").setDataSymbol(DiamondSymbol, 9) # Enable data label on the data points. Set the label format to nn%. layer.setDataLabelFormat("{value}") # Output the chart c.makeChart("symbolline.png")
推薦閱讀:
[1] 2013年度Python數據模塊
http://segmentfault.com/a/1190000000424711
[2] 55種開源數據可視化工具簡介
[3] 讓效率「爆表」的49個數據可視化工具