我用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. ***CI异常记录到日志:CodeIgniter中设计一个全局exception hook

    在CodeIgniter中,当发生异常时,经常要通知系统管理员,因此有必要在全局的高度上 捕捉异常,因此可以写一个hook, 比如在config目录的hook.php中,加入: $hook['pre_ ...

  2. BZOJ 2553 禁忌

    首先我们要考虑给定一个串,如何将他划分,使得他有最多的禁忌串 我们只需要按里面出现的禁忌串们的出现的右端点排序然后贪心就可以啦 我们建出AC自动机,在AC自动机等价于走到一个包含禁忌串的节点就划分出一 ...

  3. spring aop环绕通知

    [Spring实战]—— 9 AOP环绕通知   假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...

  4. 安装 ArcGIS Runtime SDK for Android

    ArcGIS for Android 开发:Android 平台搭建 - liyong20080101的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/liyong ...

  5. 最短路径算法之二——Dijkstra算法

    Dijkstra算法 Dijkstra算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 注意该算法要求图中不存在负权边. 首先我们来定义一个二维数组Edge[MAXN][MAXN]来存储 ...

  6. 安装Ubuntu双系统系列——64位Ubuntu安装H3C的INode客户端

    学校使用的是Inode客户端认证上网的.如果是使用Ubuntu 32位版本,可以完美地安装并能够连接到网站.但是如果安装的是Ubuntu desktop 12.10 amd64版本,则发现之前的&qu ...

  7. java ServerSocket服务端编程

    public class Test { public static void main(String[] args) throws Exception{ //1. 构造ServerSocket实例,指 ...

  8. Eclipse下Preferences解析

    General列表下: Keys设置快捷键 Appearance->Colors and Fonts->Text-Font设置字体和大小 Appearance->Startup an ...

  9. 【待填坑】bzoj上WC的题解

    之前在bzoj上做了几道WC的题目,现在整理一下 bzoj2115 去膜拜莫队的<高斯消元解xor方程组> bzoj2597 LCT维护MST bzoj1758 分数规划+树分治+单调队列 ...

  10. Sencha 基础Demo测试,三种showView的方法

    直接贴代码吧 Ext.define("build.controller.MainController",{ extend:"Ext.app.Controller" ...