我用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. 【数位DP】bzoj1026: [SCOI2009]windy数

    1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4163  Solved: 1864[Submit][Sta ...

  2. C#学习笔记(一)

    1.cmd运行devenv启动VS. 2.解决方案:公司 项目:部门 类:员工 3.右边的解决方案管理器:会自动隐藏,想让他固定的话,就点击关闭按钮中间的“自动隐藏”:可以拖动到上下左右,当出现阴影的 ...

  3. 【C++基础】 各种“虚”总结(ing...)

    虚基类,虚函数,虚析构函数,纯虚函数,虚函数表(待补充) 一.虚基类——在继承方式(public / private)之前加 virtual class B1: virtual public B0{} ...

  4. VS2005 VS2008 Manifest 配置问题总结

    一.问题 编译某个遗留工程后,运行程序时报错,“由于应用程序的配置不正确,应用程序无法启动.重新安装应用程序可能会解决这个问题.” 查看生成的Manifest文件如下: <?xml versio ...

  5. struts2 标签库 介绍

    struts2.0里的标签没有分类,只用在jsp头文件加上<%@ taglib prefix="s" uri="/struts-tags" %>就能 ...

  6. JAVA面试题:69道Spring面试题和答案

    目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP) Spring MVC Spring 概述 1. 什么是spri ...

  7. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  8. ZooKeeper 安装部署及hello world

    ZooKeeper  安装部署及hello world 先给一堆学习文档,方便以后查看官网文档地址大全: OverView(概述)http://zookeeper.apache.org/doc/r3. ...

  9. Android四大基本组件

    Android四大基本组件分别是 Activity:整个应用程序的门面,负责与用户进行交互. Service:承担大部分工作. Content Provider内容提供者:负责对外提供数据,并允许需要 ...

  10. Octave下载

    发福利啦,今天下了半天Octave都没下载下来,最后让一个香港的同学帮忙下好传过来的....放到网盘里造福大家 GNU_Octave_3.8.0-6.dmg 链接: http://pan.baidu. ...