Python畫圖學習入門教程
本文實例講述了Python畫圖的基本方法。分享給大家供大家參考,具體如下:
Python:使用matplotlib繪制圖表
python繪制圖表的方法,有個強大的類庫matplotlib,可以制作出高質量的2D和3D圖形,先記錄一下,以后慢慢學習。
matplotlib下載及API手冊地址:http://sourceforge.net/projects/matplotlib/files/matplotlib/
數(shù)學庫numpy下載及API手冊地址:http://www.scipy.org/Download
幾個繪圖的例子,來自API手冊:
1、最簡單的圖:
代碼:
#!/usr/bin/env python import matplotlib.pyplot as plt plt.plot([10, 20, 30]) plt.xlabel('tiems') plt.ylabel('numbers') plt.show()
2.餅圖:
代碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- from pylab import * # make a square figure and axes figure(1, figsize=(6,6)) ax = axes([0.1, 0.1, 0.8, 0.8]) labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' fracs = [15,30,45, 10] explode=(0, 0.05, 0, 0) pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True) title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5}) savefig('D:\\pie.png') show()
3、使用numpy庫函數(shù):
代碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt t = np.arange(0.0, 1.01, 0.01) s = np.sin(2*2*np.pi*t) plt.fill(t, s*np.exp(-5*t), 'r') plt.grid(True) #保存為PDF格式,也可保存為PNG等圖形格式 plt.savefig('D:\\test.pdf') plt.show()
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結》、《Python圖片操作技巧總結》、《Python數(shù)據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
PyCharm出現(xiàn)Error:Python?packaging?tool?'setuptools&apo
這篇文章主要給大家介紹了關于PyCharm出現(xiàn)Error:Python?packaging?tool?'setuptools'?not?found的解決辦法,文中通過圖文及代碼介紹的非常詳細,需要的朋友可以參考下2023-12-12Python的幾個高級語法概念淺析(lambda表達式閉包裝飾器)
本文主要記錄自己對幾個高級語法概念的理解:匿名函數(shù)、lambda表達式、閉包、裝飾器。這幾個概念并非Python特有,但本文只限于用Python做說明2016-05-05