使用python中的Matplotlib繪圖樣本__python

來源:互聯網
上載者:User

當我們按照前一篇博文

http://blog.csdn.net/tao_627/article/details/44004541

配置好python的繪圖環境後,下面給出幾個有代表性的例子:

一.繪製柱狀圖

#!/usr/bin/env_python#encoding: utf-8import matplotlib.pyplot as pltdef bar_chart_generator():    l=[1,2,3,4,5]    h=[20,14,38,27,9]    w=[0.1,0.2,0.3,0.4,0.5]    b=[1,2,3,4,5]    fig=plt.figure()    ax=fig.add_subplot(111)    rects=ax.bar(l,h,w,b)    plt.show()bar_chart_generator()



二.繪製曲線圖

#!/usr/bin/env_python#encoding: utf-8#usage: python curve_demo.pyimport matplotlib.pyplot as pltimport numpy as np#To draw y=x^2(-3<=x<=3)x = np.arange(-3,3.5,0.5)y = [ele**2 for ele in x]z = [ele *2 for ele in x]fig = plt.figure(1)ax = fig.add_subplot(211)line1 = ax.plot(x,y,'ro-')ax = fig.add_subplot(212)line2 = ax.plot(x,z,'g-')plt.show()


三.繪製折線圖

#!/usr/bin/env_python#encoding: utf-8import numpy as npimport pylab as plfrom StringIO import StringIOdata_str = """2012-04-01_02 682012-04-01_05 702012-04-01_08 692012-04-01_11 712012-04-01_14 722012-04-01_20 702012-04-02_02 712012-04-02_05 702012-04-02_08 692012-04-02_11 712012-04-02_14 692012-04-02_20 712012-04-03_02 742012-04-03_05 732012-04-03_08 772012-04-03_11 702012-04-03_14 712012-04-03_20 702012-04-04_02 702012-04-04_05 722012-04-04_08 722012-04-04_11 692012-04-04_14 712012-04-04_20 692012-04-05_02 75"""data = np.loadtxt(StringIO(data_str), dtype=np.dtype([("t", "S13"),("v", float)]))datestr = np.char.replace(data["t"], "_", " ")t = pl.datestr2num(datestr)v = data["v"]pl.plot_date(t, v, fmt="-o")pl.subplots_adjust(bottom=0.3)ax = pl.gca()ax.fmt_xdata = pl.DateFormatter('%Y-%m-%d %H:%M:%S')pl.xticks(rotation=90)pl.xticks(t, datestr) # 如果以資料點為刻度,則注釋掉這一行ax.xaxis.set_major_formatter(pl.DateFormatter('%Y-%m-%d %H'))pl.grid()pl.show()


參考文獻

[1].http://blog.sina.com.cn/s/blog_68b606350101ryao.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.