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

三、实验小结

Python的安装比较简单,但是numpy、matplotlib、scipy的安装并没有预期的简单,首先版本得对应安装的python版本,而且分32和64位,资源不容易找,安装成功后还要装其他的东西。至于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)

利用python进行折线图,直方图和饼图的绘制的更多相关文章

  1. 利用JFreeChart生成折线图 (4) (转自 JSP开发技术大全)

    利用JFreeChart生成折线图 (4) (转自 JSP开发技术大全) 14.4 利用JFreeChart生成折线图 通过JFreeChart插件,既可以生成普通效果的折线图,也可以生成3D效果的折 ...

  2. Python制作折线图

    利用python的第三方包Pygal制作简单的折线图. 申明:本文仅供学习交流使用.源码大部分来自<python编程从入门到实践>:如有侵权,请联系我删除. 1 #!usr/bin/env ...

  3. python matplotlib 折线图

    1.绘制折线图,去上和右边框,显示中文 import numpy as np import matplotlib.pyplot as plt #plt.style.use('default') #pl ...

  4. 利用Python+pyecharts+tushare图形化展示股票历史财务信息

    在微信或其他平台上,经常能看到别人推荐股票,分析的头头是道,让自己懊恼于没有早点关注到这只股票,好像错失了几个亿.但是投资股票又忌讳听消息跟风,总不能看到别人推荐自己就无脑买入. 看到了一只股票,自己 ...

  5. 宅男福利--利用Python简单爬图

    Ver beta..代码粗陋. 使用说明以Windows为例, Python版本为2.7.6 确认你电脑已经安装了Python, Windows默认安装路径为C:\Python27.如果没有安装,先下 ...

  6. python绘折线图

    # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt #X轴,Y轴数据 y = [0.3,0.4,2,5 ...

  7. 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图

    1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...

  8. python 绘图---2D、3D散点图、折线图、曲面图

    python中绘制2D曲线图需要使用到Matplotlib,Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形,通过 Matplo ...

  9. 使用python制作动图

    利用python制作gif图 引言 当写文章时候,多张图片会影响排版,可以考虑制作gif图 准备 pip install imageio 代码 # This is a sample Python sc ...

随机推荐

  1. 团体程序设计天梯赛-练习集L1-016. 查验身份证

    L1-016. 查验身份证 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个合法的身份证号码由17位地区.日期编号和顺序编号 ...

  2. ural 1123

    找大于等于原数的最小回文数字  代码比较烂........... #include <iostream> #include <cstdio> #include <cstr ...

  3. uva 348

    dp还是比较容易  关键是输出路径 .... #include <cstdlib> #include <cstdio> #include <cstring> #de ...

  4. Codeforces Round #243 (Div. 2) A~C

    题目链接 A. Sereja and Mugs time limit per test:1 secondmemory limit per test:256 megabytesinput:standar ...

  5. ???????????? no permissions

    1.一手鞋地址 google http://developer.android.com/tools/device.html 我处理的方法如下: 我的问题: android的版卡 在Ubuntu12.0 ...

  6. linux Ubuntu安装后没有引导 解决方案

    用EasyBCD添加ubuntu grub2引导,适用于12.04 及之前版本的ubuntu安装好easybcd后运行,之后看图

  7. Servlet课程0426(八)Servlet分页技术

    Welcome.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; import java ...

  8. ANDROID_MARS学习笔记_S01_008Linear_layout例子

    1.netstone_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  9. js setTimeout深度递归后完成回调

    setTimout原型: iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])     setTimeout有两种形式 se ...

  10. C#解析.msg文件(outlook文件)

    起因 有一批邮件(700+),全是 .msg 文件,是同群发邮件产生的退信,这些退信需要作分析,得出退信产生的原因. 解决方法 在网上搜了一下发现 .msg文件有其自己的格式,MS提供了格式说明,自己 ...