Python进阶(三十八)利用matplotlib 进行折线图,直方图和饼图的绘制

一、前言

我用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()

效果图如下图:

这里写图片描述

三、柱状图绘制

再画柱状图,代码如下:

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_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()

效果图如下图:

这里写图片描述

五、完整代码

# -*- 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)

这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No Silver Bullet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值