Python之matplotlib学习(四)

例子12:ipython使用--pylab参数,默认加入matplotlib模块

[root@typhoeus79 guosong]# ipython  --pylab
WARNING: IPython History requires SQLite, your history will not be saved
Python 2.7.3 (default, Nov 27 2012, 17:47:24) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

Welcome to pylab, a matplotlib-based Python environment [backend: agg].
For more information, type 'help(pylab)'.

In [1]: print matplotlib.__version__
1.3.1

 例子13:输出多个图

#!/usr/bin/env python2.7
#-*- coding:utf8 -*-

import os
import sys

import matplotlib as mpl
mpl.use('Agg')#before importing pyplot

import matplotlib.pyplot as plt
from matplotlib.dates import datestr2num,DateFormatter

import matplotlib.dates as dates
import matplotlib.ticker as tk

mpl.rcParams['figure.figsize'] = (12,16)


sys.path.append("./lib")
from mysql_base import MySQLBase


def get_blog_size(cursor,size_port_list):
    data_size_list = []

    for size_port in size_port_list:

        sql = "select db_size,riqi from port_size where port = %s order by riqi DESC limit 60" % size_port

        cursor.execute(sql)

        result = cursor.fetchall()

        riqi_list = []
        db_size_list = []

        for row in result:
            riqi_list.append(str(row['riqi']))
            db_size_list.append(float(row['db_size']))

        riqi_list = riqi_list[::-1]
        db_size_list = db_size_list[::-1]

        data_size_tuple = (size_port,riqi_list,db_size_list)
        data_size_list.append(data_size_tuple)

    return data_size_list


def draw(data_size_list):

    fig = plt.figure(2)
    idx = 411
  #第一位是行数,第二位是列数 for data_size in data_size_list: (port,riqi_list,db_size_list) = data_size ax=plt.subplot(idx) formatter = DateFormatter('%Y-%m-%d') ax.xaxis.set_major_formatter(formatter) plt.sca(ax) ax.plot_date(datestr2num(riqi_list),db_size_list,'-',label="(GB)") title = "%s_Data_Size" % port plt.title(title) plt.legend(loc='best') plt.grid(True) idx = idx + 1 plt.subplots_adjust(left=0.08, right=0.95, wspace=0.25, hspace=1) plt.savefig("blog_data_size.pdf",dpi=200) def main(): host = "XXXX" port = "xxxx" user = "xxx" passwd="xxxx" db = "xxx" conn = MySQLBase(host=host,port=port,user=user,passwd=passwd,db=db) cursor = conn.cursor() port_list = [xxxx] print port_list data_size_list = get_blog_size(cursor,port_list) draw(data_size_list) cursor.close() conn.close() if __name__ == '__main__': main()

输出结果:

原文地址:https://www.cnblogs.com/gsblog/p/3427266.html