利用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()
最后如下图:

再画柱状图,代码如下:
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进行折线图,直方图和饼图的绘制的更多相关文章
- 利用JFreeChart生成折线图 (4) (转自 JSP开发技术大全)
利用JFreeChart生成折线图 (4) (转自 JSP开发技术大全) 14.4 利用JFreeChart生成折线图 通过JFreeChart插件,既可以生成普通效果的折线图,也可以生成3D效果的折 ...
- Python制作折线图
利用python的第三方包Pygal制作简单的折线图. 申明:本文仅供学习交流使用.源码大部分来自<python编程从入门到实践>:如有侵权,请联系我删除. 1 #!usr/bin/env ...
- python matplotlib 折线图
1.绘制折线图,去上和右边框,显示中文 import numpy as np import matplotlib.pyplot as plt #plt.style.use('default') #pl ...
- 利用Python+pyecharts+tushare图形化展示股票历史财务信息
在微信或其他平台上,经常能看到别人推荐股票,分析的头头是道,让自己懊恼于没有早点关注到这只股票,好像错失了几个亿.但是投资股票又忌讳听消息跟风,总不能看到别人推荐自己就无脑买入. 看到了一只股票,自己 ...
- 宅男福利--利用Python简单爬图
Ver beta..代码粗陋. 使用说明以Windows为例, Python版本为2.7.6 确认你电脑已经安装了Python, Windows默认安装路径为C:\Python27.如果没有安装,先下 ...
- python绘折线图
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt #X轴,Y轴数据 y = [0.3,0.4,2,5 ...
- 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图
1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...
- python 绘图---2D、3D散点图、折线图、曲面图
python中绘制2D曲线图需要使用到Matplotlib,Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形,通过 Matplo ...
- 使用python制作动图
利用python制作gif图 引言 当写文章时候,多张图片会影响排版,可以考虑制作gif图 准备 pip install imageio 代码 # This is a sample Python sc ...
随机推荐
- CQRS学习——一个例子(其六)
[先上链接:http://pan.baidu.com/s/1o62AHbc ] 多图杀猫 先用一组图看看实现的功能: 添加一个功能 假定现在要添加一个书本录入的功能,那么执行如下的操作: 1.添加Co ...
- 想让安卓 APP 如丝般顺滑?
随着安卓手机市场占有率的节节攀升,随便在大街上找几个人估计 80% 用的都是安卓手机吧!用安卓手机的人这么多,不知道大家是否曾经感觉到过 APP 卡顿.死机?是否遇到应用程序无响应.闪退?本文就为大家 ...
- VS2005 VS2008 Manifest 配置问题总结
一.问题 编译某个遗留工程后,运行程序时报错,“由于应用程序的配置不正确,应用程序无法启动.重新安装应用程序可能会解决这个问题.” 查看生成的Manifest文件如下: <?xml versio ...
- 吐槽C++
个人感觉,在c++ 道路的学习路上,遇到很多的坎坷,现在回想起来,最关键一点就是 c++知识点繁杂很多,教科书很多知识点都没有提到. 但是在实际工作中,这些没有提到的知识点,却又经常会用到(或者看开源 ...
- Source Insight 安装使用
习惯了在source insight下编辑阅读源码,在linux下用vi总是用不好 ,还是在Ubuntu上用回熟悉的source insight. 在Ubuntu中,安装Windows程序用wine, ...
- Ubuntu 12.04上编译Vim7.4的时候遇到“no terminal library found”问题
错误如下: no terminal library foundchecking for tgetent()... configure: error: NOT FOUND! You need ...
- Redpine的Lite-Fi解决方案获Wi-Fi CERTIFIED认证
应用微电路公司(AMCC)和Redpine Signals日前共同宣布,已合作开发出新一代基于Power Architecture的嵌入式Wi-Fi连接性解决方案,目前双方已经在AMCC的PowerP ...
- poj 3321 Apple Tree(一维树状数组)
题目:http://poj.org/problem?id=3321 题意: 苹果树上n个分叉,Q是询问,C是改变状态.... 开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号 白 ...
- poj 1836 Alignment(dp)
题目:http://poj.org/problem?id=1836 题意:最长上升子序列问题, 站队,求踢出最少的人数后,使得队列里的人都能看到 左边的无穷远处 或者 右边的无穷远处. 代码O(n^2 ...
- FormsAuthentication 登录兼容 IE11 保存cookie
现象:使用FormsAuthentication进行登录验证,在IE11客户端无法保存cookie 解决方法:在web.config中的forms中增加cookieless="UseCook ...