歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python之PyChart畫圖方法

Python之PyChart畫圖方法

日期:2017/3/1 10:21:20   编辑:Linux編程
其實,Python繪圖的方式很多,也有很多開源不錯的模塊可以選擇,比如常用於科學計算的MatplotlibCairoplot(需要翻牆)、ChacoPython Google ChartPyChapyOFC2PyChartPLplotReportLabVPython等等。

這裡,我們介紹下PyChart模塊,其繪圖還是比較方便的,而且繪出的圖像也還不錯。我們除了需要安裝PyChart模塊之外,還需要安裝Ghostscript和GSview,為了方便大家,PyChart與 GSview下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/5月/29日/Python之PyChart畫圖方法/

OK,安裝完之後,看一個簡單的示例代碼:

  1. # http://www.linuxidc.com/
  2. from pychart import *
  3. class zap_y_coord(linear_coord.T):
  4. def get_canvas_pos(self, size, val, min, max):
  5. if val <= 70:
  6. return linear_coord.T.get_canvas_pos(self, size, val, 0, 110)
  7. elif val <= 240:
  8. return linear_coord.T.get_canvas_pos(self, size, 70, 0, 110)
  9. else:
  10. return linear_coord.T.get_canvas_pos(self, size, val - 170, 0, 110)
  11. def get_tics(self, min, max, interval):
  12. tics = linear_coord.T.get_tics(self, min, max, interval)
  13. return [x for x in tics if x < 65 or x > 256]
  14. # can = canvas.default_canvas()
  15. can = canvas.init('flykite.png')
  16. theme.scale_factor=3
  17. theme.use_color=True
  18. theme.reinitialize()
  19. data = [(10, 20, 30, 5), (20, 265, 33, 5),
  20. (30, 255, 30, 5), (40, 45, 51, 7), (50, 25, 27, 3)]
  21. chart_object.set_defaults(area.T, size = (150, 120), y_range = (0, 280),
  22. y_coord = zap_y_coord(),
  23. x_coord = category_coord.T(data, 0))
  24. chart_object.set_defaults(bar_plot.T, data = data)
  25. bar_plot.fill_styles.reset();
  26. plot1=bar_plot.T(label="foo", cluster=(0,3))
  27. plot2=bar_plot.T(label="bar", hcol=2, cluster=(1,3))
  28. plot3=bar_plot.T(label="baz", hcol=3, cluster=(2,3))
  29. ar = area.T(loc=(250,0),
  30. x_axis=axis.X(label="X label", format="/a-30{}%d"),
  31. y_axis=axis.Y(label="Y label", tic_interval=10))
  32. ar.add_plot(plot1, plot2, plot3)
  33. ar.draw()
  34. for x in (ar.x_pos(10) - 20, ar.x_pos(20)- 10, ar.x_pos(30) - 10):
  35. zap.zap_horizontally(can, line_style.default, fill_style.white,
  36. x, ar.y_pos(65), x+16, ar.y_pos(65) + 4, 4, 4)

為了生成png圖片,使用以下命令運行程序:

  1. python flykite.py --format=png

圖片效果如下:

650) this.width=650;" border=0>

Copyright © Linux教程網 All Rights Reserved