python 使用 matplotlib.pyplot来画柱状图和饼图

导入包

import matplotlib.pyplot as plt

柱状图

最简柱状图

# 显示高度
def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x()+rect.get_width()/2.- 0.2, 1.03*height, ‘%s‘ % int(height))

name_list = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘]
num_list = [33, 44, 53, 16, 11, 17, 17, 10]
autolabel(plt.bar(range(len(num_list)), num_list, color=‘rgb‘, tick_label=name_list))
plt.show()

结果

堆叠柱状图

# 显示高度
def autolabel(rects1, rects2):
    i = 0
    for rect1 in rects1:
        rect2 = rects2[i]
        i += 1
        height = rect1.get_height() + rect2.get_height()
        plt.text(rect1.get_x()+rect1.get_width()/2. - 0.1, 1.03*height, ‘%s‘ % int(height))

name_list = [‘A‘, ‘B‘, ‘C‘, ‘D‘]
num_list = [10, 15, 16, 28]
num_list2 = [10, 12, 18, 26]
z1 = plt.bar(range(len(num_list)), num_list, label=‘1‘, fc=‘b‘)
z2 = plt.bar(range(len(num_list)), num_list2, bottom=num_list, label=‘2‘, tick_label=name_list, fc=‘g‘)
autolabel(z1, z2)
plt.legend()
plt.show()

结果

并列柱状图

name_list = [‘A‘, ‘B‘, ‘C‘, ‘D‘]
num_list = [10, 15, 16, 28]
num_list2 = [10, 12, 18, 26]
x = list(range(len(num_list)))
total_width, n = 0.8, 2
width = total_width / n
plt.bar(x, num_list, width=width, label=‘1‘, fc=‘b‘)
for i in range(len(x)):
    x[i] += width
plt.bar(x, num_list2, width=width, label=‘2‘, tick_label=name_list, fc=‘g‘)
plt.legend()
plt.show()

结果

饼图

最简饼图

name_list = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘]
num_list = [33, 44, 53, 6,11, 7, 7, 10, 3, 1]
# 保证圆形
plt.axes(aspect=1)
plt.pie(x=num_list, labels=name_list, autopct=‘%3.1f %%‘)
plt.show()

结果

带切割的饼图

# 圆形
plt.figure(1, figsize=(6, 6))
name_list = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘]
num_list = [33, 44, 53, 30, 60]
expl = [0, 0, 0, 0, 1]
colors = [‘pink‘, ‘blue‘, ‘yellow‘, ‘green‘, ‘red‘]
plt.pie(x=num_list,explode=expl, labels=name_list, autopct=‘%3.1f %%‘, colors=colors, pctdistance=0.3, shadow=True)
plt.show()

结果

原文地址:https://www.cnblogs.com/zhhfan/p/9971757.html

时间: 2024-08-11 12:34:54

python 使用 matplotlib.pyplot来画柱状图和饼图的相关文章

【python】matplotlib.pyplot入门

matplotlib.pyplot介绍 matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表. matplotlib.pyplot是命令行式函数的集合,每一个函数都对图像作了修改,比如创建图形,在图像上创建画图区域,在画图区域上画线,在线上标注等. 下面简单介绍一下pyplot的基本使用: (1)使用plot()函数画图 plot()为画线函数,下面的小例子给ploy()一个列表数据[1,2,3,4],matplotlib假设它是y轴的数值序列,然

Solution for Python Extention matplotlib.pyplot loading Error

When I execute the following python instruction in python shell >>>import matplotlib.pyplot as plt error occured and the error message show as follows,"UnicodeDecodeError: 'ascii' codec can't decode byte 0xea in position 0: ordinal not in ra

Python学习-windows安装Python以及matplotlib.pyplot包

引文: Python自带了许多的库文件,其中matplotlib可以做出类似于MATLAB和R语言一样绘制出很好的图形功能,下面介绍下怎么安装这个包,因为自己安装的时候很多地方都出错了. 环境: Windows X64 python2.7.5 说明:虽然电脑是64位系统,但电脑装的python依旧是32位的. 1 python下载和安装 1.1 python下载 首先下载python2.7.5:https://www.python.org/downloads/windows/ 或者到我的CSDN

Python中用matplotlib.pyplot画图总结

1. import numpy as np import matplotlib.pyplot as plt def f1(t):     #根据横坐标t,定义第一条曲线的纵坐标     return np.exp(-t)*np.cos(2*np.pi*t) def f2(t):     #根据横坐标t,定义第二条曲线的纵坐标     return np.sin(2*np.pi*t)*np.cos(3*np.pi*t) #定义很坐标的值,来自于np.arange(0.0,5.0,0.02), #.

[转]用Matplotlib绘制 折线图 散点图 柱状图 圆饼图

Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形. 安装 Matplotlib并不是Python的默认组件,需要额外安装. 官方下载地址 http://matplotlib.org/downloads.html 必须下载与自己的Python版本,操作系统类型对应的安装包.如Windows 64位+Python3.3,应该下载matplotlib-1.3.1.win-amd64-py3.3.exe 第

Android----画柱状图和饼图

使用GraphicalView画柱状图和饼图 一.achartengine库的下载,下载地址: http://code.google.com/p/achartengine/downloads/list 下载完成后,把jar文件粘贴到libs文件夹 achartengine是为Android设计的绘图工具库. 二.在android项目中如何使用 先定义一个GraphicalView GraphicalView graphicalView; 然后,从chartfactory获取 graphicalV

python画柱状图并且输出到html文件

import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltfrom Cstring import StringIO y = [3, 10, 7, 5, 3, 4.5, 6, 8.1] N = len(y) x = range(N) width = 1/1.5 plt.bar(x, y, width, color="blue") io=StringIO()plt.savefie(io,format="

Python 中 plt 画柱状图和折线图

1. 背景 Python在一些数据可视化的过程中需要使用 plt 函数画柱状图和折线图. 2. 导入 import matplotlib.pyplot as plt 3. 柱状图 array= np.array(array) plt.hist(array, bins=50,facecolor="red", edgecolor="red" ,linewidth=5,alpha=0.7) plt.xlabel("") plt.ylabel("

使用Python操作neo4j和画柱状图

前言 毕业设计里要用到neo4j和柱状图来对数据进行可视化,踩了几天坑,今天填一下. Neo4J 这是一个图像数据库,接触到这东西发现挺有意思的,比学MySQL有意思多了. 安装 1,从官网下载,但是速度奇慢,可能需要搭梯子. 2,当然是百度搜一搜了,反正挺多的,要么可以用我这个(链接:https://pan.baidu.com/s/1FUmJsA_6UR6Kgkrs7f_OMQ 提取码:5t06),适用于JDK1.8 下载好安装包之后,解压到某一个目录,如果跟我一样只进行简单的可视化那不需要修