利用python進行折線圖,直方圖和餅圖的繪制


 

我用10個國家某年的GDP來繪圖,數據如下:

labels   = ['USA', 'China', 'India', 'Japan', 'Germany', 'Russia', 'Brazil', 'UK', 'France', 'Italy']

quants   = [15094025.0, 11299967.0, 4457784.0, 4440376.0, 3099080.0, 2383402.0, 2293954.0, 2260803.0, 2217900.0, 1846950.0]

首先繪制折線圖,代碼如下:

def draw_line(labels,quants):

    ind = np.linspace(0,9,10)

    fig = plt.figure(1)

    ax  = fig.add_subplot(111)

    ax.plot(ind,quants)

    ax.set_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})

    ax.set_xticklabels(labels)

    plt.grid(True)

plt.show()

最后如下圖:

clip_image002[4]

再畫柱狀圖,代碼如下:

def draw_bar(labels,quants):

    width = 0.4

    ind = np.linspace(0.5,9.5,10)

    # make a square figure

    fig = plt.figure(1)

    ax  = fig.add_subplot(111)

    # Bar Plot

    ax.bar(ind-width/2,quants,width,color='green')

    # Set the ticks on x-axis

    ax.set_xticks(ind)

    ax.set_xticklabels(labels)

    # labels

    ax.set_xlabel('Country')

    ax.set_ylabel('GDP (Billion US dollar)')

    # title

    ax.set_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})

    plt.grid(True)

plt.show()

clip_image004[4]

最后畫餅圖,代碼如下:

def draw_pie(labels,quants):

    plt.figure(1, figsize=(6,6))

    # For China, make the piece explode a bit

    expl = [0,0.1,0,0,0,0,0,0,0,0]

    # Colors used. Recycle if not enough.

    colors  = ["blue","red","coral","green","yellow","orange"]

    # autopct: format of "percent" string;

    plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)

    plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})

plt.show()

clip_image006[4]

三、實驗小結

Python的安裝比較簡單,但是numpymatplotlibscipy的安裝並沒有預期的簡單,首先版本得對應安裝的python版本,而且分3264位,資源不容易找,安裝成功后還要裝其他的東西。至於matplitlib的畫圖感覺還是比較方便的,初學python,雖然整體簡潔了很多,但是python的格式的要求過於嚴格,尤其是縮進等,初學者查了好久都檢查不出錯誤但后來就又稀里糊塗運行成功了,比較抓狂。

 

 

附錄:完整代碼:

# -*- coding: gbk -*-

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

 

def draw_pie(labels,quants):

    # make a square figure

    plt.figure(1, figsize=(6,6))

    # For China, make the piece explode a bit

    expl = [0,0.1,0,0,0,0,0,0,0,0]

    # Colors used. Recycle if not enough.

    colors  = ["blue","red","coral","green","yellow","orange"]

    # Pie Plot

    # autopct: format of "percent" string;

    plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)

    plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})

    plt.show()

def draw_bar(labels,quants):

    width = 0.4

    ind = np.linspace(0.5,9.5,10)

    # make a square figure

    fig = plt.figure(1)

    ax  = fig.add_subplot(111)

    # Bar Plot

    ax.bar(ind-width/2,quants,width,color='green')

    # Set the ticks on x-axis

    ax.set_xticks(ind)

    ax.set_xticklabels(labels)

    # labels

    ax.set_xlabel('Country')

    ax.set_ylabel('GDP (Billion US dollar)')

    # title

    ax.set_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})

    plt.grid(True)

    plt.show()

def draw_line(labels,quants):

    ind = np.linspace(0,9,10)

    fig = plt.figure(1)

    ax  = fig.add_subplot(111)

    ax.plot(ind,quants)

    ax.set_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})

    ax.set_xticklabels(labels)

    plt.grid(True)

    plt.show()

# quants: GDP

# labels: country name

labels   = ['USA', 'China', 'India', 'Japan', 'Germany', 'Russia', 'Brazil', 'UK', 'France', 'Italy']

quants   = [15094025.0, 11299967.0, 4457784.0, 4440376.0, 3099080.0, 2383402.0, 2293954.0, 2260803.0, 2217900.0, 1846950.0]

draw_pie(labels,quants)

#draw_bar(labels,quants)

#draw_line(labels,quants)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM