Python进阶(三十八)-数据可视化の利用matplotlib 进行折线图,直方图和饼图的绘制
来源:互联网 发布:大数据的4v特征变化快 编辑:程序博客网 时间:2024/06/04 06:23
Python进阶(三十八)-数据可视化の利用matplotlib 进行折线图,直方图和饼图的绘制
我用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()
效果图如下图:
附录:完整代码:
# -*- coding: gbk -*-import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mpldef 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 namelabels = ['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)
1 0
- Python进阶(三十八)-数据可视化の利用matplotlib 进行折线图,直方图和饼图的绘制
- 利用python进行折线图,直方图和饼图的绘制
- python 数据可视化 matplotlib学习一:绘制简单的折线图
- Python数据可视化:Matplotlib 直方图、箱线图、条形图、热图、折线图、散点图。。。
- python数据可视化(matplotlib,pandas绘图,直方图,散点图,柱状图,折线图,箱线图)
- python数据可视化——利用pyplot绘制折线图和散点图
- 利用DevExpress的WebChartControl绘制直方图,折线图和饼状图
- Python进阶(四十)-数据可视化の使用matplotlib进行绘图
- python数据分析——matplotlib生成折线图,散点图和直方图
- python matplotlib从文件中读取数据绘制折线图
- 数据可视化1---直方图-折线图
- python实战二:使用CSV数据绘制带数据标志的折线图(matplotlib)
- python使用matplotlib绘制折线图教程
- 【Python-3.5】matplotlib绘制气温折线图
- Matplotlib绘制折线图
- Python进阶(三十九)-数据可视化の使用matplotlib进行绘图分析数据
- 【python 可视化】python利用matplotlib库绘制饼图案例
- 使用matplotlib绘制简单的折线图
- 【POJ】2449 Remmarguts' Date k短路(最短路径+A*)
- 神奇的口袋
- Linux shell环境
- Java内存模型与线程
- ScrollView中嵌套ListView置顶问题
- Python进阶(三十八)-数据可视化の利用matplotlib 进行折线图,直方图和饼图的绘制
- 深入字节码操作:使用ASM和Javassist创建审核日志
- SQL优化之道
- 讨论时间复杂度
- (简单排序dfs)今年暑假不AC
- 浅谈继承
- Qt中进度条窗口
- 无插件清理WordPress数据库的方法
- REST Service 开发