利用python进行折线图,直方图和饼图的绘制
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了利用python进行折线图,直方图和饼图的绘制,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3625字,纯文字阅读大概需要6分钟。
内容图文
我用 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)
原文:http://www.cnblogs.com/hustlx/p/5264562.html
内容总结
以上是互联网集市为您收集整理的利用python进行折线图,直方图和饼图的绘制全部内容,希望文章能够帮你解决利用python进行折线图,直方图和饼图的绘制所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。