热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用python画折线图、柱状图、饼图

labels[‘USA’,‘China’,‘India’,‘Japan’,‘Germany’,‘Russia’,‘Brazil’,‘UK’,‘France’,

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,18469]

1.折线图

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()

这里写图片描述

2.柱状图

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()

这里写图片描述

3.饼图

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()

这里写图片描述

ps:需要用到numpy、matplotlib、scipy
所以需要加上import
用spyder
完整代码如下:

import numpy as np  
import matplotlib.pyplot as plt
import matplotlib as mpl
以上三个代码块
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)

推荐阅读
author-avatar
雨润风华_684
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有