生活随笔
收集整理的這篇文章主要介紹了
Matplotlib绘图库初探
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Matplotlib是Python的2D&3D繪圖庫,產生各種已經拷貝格式和交互幻劍中跨平臺形式的印刷質量圖標。Matplot語法與Matlab相似,繪圖繪圖功能強大,而且十分容易上手。
“個人永遠不能超過集體的力量”(Ken Blanchard)。Python強大的原因之一就在于其開源,有很多優秀的程序員為其提供了豐富的類庫。Matplotlib就是其中之一,但他的創始人John D. Hunter英年早逝,在今年8月份死于治療癌癥引起的并發癥。向這位優秀的程序員致敬!
安裝matplot之前先要安裝Numpy。
Numpy也是python的一個擴展包,提供基礎的科學計算,包括:
- 強大的N維矩陣對象
- C/C++ 和 Fortran 代碼集成工具
- 有用的線性代數、傅立葉轉換和隨機數生成函數
Numpy的下載地址:http://scipy.org/Download Matlabplot的下載地址:https://github.com/matplotlib/matplotlib/downloads 也可以從我的csdn資源下載(附有說明文檔): 安裝都很簡單,一路雙擊就可以~
以下是一個簡單的繪制正弦三角函數y=sin(x)的例子。
[python]?view plaincopy
?? from?pylab?import?*?? x_values?=?arange(0.0,?math.pi?*?4,?0.01)?? y_values?=?sin(x_values)?? plot(x_values,?y_values,?linewidth=1.0)?? xlabel('x')?? ylabel('sin(x)')?? title('Simple?plot')?? grid(True)?? savefig("sin.png")?? show()?? 效果如圖:
pylab的plot函數與matlab很相似,也可以在后面增加屬性值,可以用
[python]?view plaincopy
help(pylab.plot)?? 查看說明:
例如用‘r*’,即紅色,星形來畫圖:
[cpp]?view plaincopy
import?os?? import?math?? import?pylab?? y_values?=?[]?? x_values?=?[]?? num?=?0.0?? #collect?both?num?and?the?sine?of?num?in?a?list?? while?num?<?math.pi?*?4:?? ????y_values.append(math.sin(num))?? ????x_values.append(num)?? ????num?+=?0.1?? ?? pylab.plot(x_values,y_values,'r*')?? pylab.show()?? Matplot中可以使用Latex來編輯公式。比如最上面那個Matplotlib的logo,背景的公式就是使用的Latex:
[python]?view plaincopy
? ? ?? ?? import?numpy?as?np?? import?matplotlib?as?mpl?? import?matplotlib.pyplot?as?plt?? import?matplotlib.cm?as?cm?? ?? mpl.rcParams['xtick.labelsize']?=?10?? mpl.rcParams['ytick.labelsize']?=?12?? mpl.rcParams['axes.edgecolor']?=?'gray'?? ?? ?? axalpha?=?0.05?? ?? figcolor?=?'white'?? dpi?=?80?? fig?=?plt.figure(figsize=(6,?1.1),dpi=dpi)?? fig.figurePatch.set_edgecolor(figcolor)?? fig.figurePatch.set_facecolor(figcolor)?? ?? ?? def?add_math_background():?? ????ax?=?fig.add_axes([0.,?0.,?1.,?1.])?? ?? ????text?=?[]?? ????text.append((r"$W^{3\beta}_{\delta_1?\rho_1?\sigma_2}?=?U^{3\beta}_{\delta_1?\rho_1}?+?\frac{1}{8?\pi?2}?\int^{\alpha_2}_{\alpha_2}?d?\alpha^\prime_2?\left[\frac{?U^{2\beta}_{\delta_1?\rho_1}?-?\alpha^\prime_2U^{1\beta}_{\rho_1?\sigma_2}?}{U^{0\beta}_{\rho_1?\sigma_2}}\right]$",?(0.7,?0.2),?20))?? ????text.append((r"$\frac{d\rho}{d?t}?+?\rho?\vec{v}\cdot\nabla\vec{v}?=?-\nabla?p?+?\mu\nabla^2?\vec{v}?+?\rho?\vec{g}$",?? ????????????????(0.35,?0.9),?20))?? ????text.append((r"$\int_{-\infty}^\infty?e^{-x^2}dx=\sqrt{\pi}$",?? ????????????????(0.15,?0.3),?25))?? ?????? ?????? ????text.append((r"$F_G?=?G\frac{m_1m_2}{r^2}$",?? ????????????????(0.85,?0.7),?30))?? ????for?eq,?(x,?y),?size?in?text:?? ????????ax.text(x,?y,?eq,?ha='center',?va='center',?color="#11557c",?alpha=0.25,?? ????????????????transform=ax.transAxes,?fontsize=size)?? ????ax.set_axis_off()?? ????return?ax?? ?? def?add_matplotlib_text(ax):?? ????ax.text(0.95,?0.5,?'matplotlib',?color='#11557c',?fontsize=65,?? ???????????????ha='right',?va='center',?alpha=1.0,?transform=ax.transAxes)?? ?? def?add_polar_bar():?? ????ax?=?fig.add_axes([0.025,?0.075,?0.2,?0.85],?polar=True)?? ?? ?? ????ax.axesPatch.set_alpha(axalpha)?? ????ax.set_axisbelow(True)?? ????N?=?7?? ????arc?=?2.?*?np.pi?? ????theta?=?np.arange(0.0,?arc,?arc/N)?? ????radii?=?10?*?np.array([0.2,?0.6,?0.8,?0.7,?0.4,?0.5,?0.8])?? ????width?=?np.pi?/?4?*?np.array([0.4,?0.4,?0.6,?0.8,?0.2,?0.5,?0.3])?? ????bars?=?ax.bar(theta,?radii,?width=width,?bottom=0.0)?? ????for?r,?bar?in?zip(radii,?bars):?? ????????bar.set_facecolor(cm.jet(r/10.))?? ????????bar.set_alpha(0.6)?? ?? ????for?label?in?ax.get_xticklabels()?+?ax.get_yticklabels():?? ????????label.set_visible(False)?? ?? ????for?line?in?ax.get_ygridlines()?+?ax.get_xgridlines():?? ????????line.set_lw(0.8)?? ????????line.set_alpha(0.9)?? ????????line.set_ls('-')?? ????????line.set_color('0.5')?? ?? ????ax.set_yticks(np.arange(1,?9,?2))?? ????ax.set_rmax(9)?? ?? if?__name__?==?'__main__':?? ????main_axes?=?add_math_background()?? ????add_polar_bar()?? ????add_matplotlib_text(main_axes)?? ????plt.show()?? (轉載請注明作者和出處:http://blog.csdn.net/xiaowei_cqu?未經允許請勿用于商業用途)
總結
以上是生活随笔為你收集整理的Matplotlib绘图库初探的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。