【Python作图】柱图,饼图,箱线图

直方图-hist

 matplotlib.pyplot.hist(  
    x, bins=10, range=None, normed=False,   
    weights=None, cumulative=False, bottom=None,   
    histtype=u'bar', align=u'mid', orientation=u'vertical',   
    rwidth=None, log=False, color=None, label=None, stacked=False,   
    hold=None, **kwargs)  

x : (n,) array or sequence of (n,) arrays

这个参数是指定每个bin(箱子)分布的数据,对应x轴

bins : integer or array_like, optional

这个参数指定bin(箱子)的个数,也就是总共有几条条状图

normed : boolean, optional

这个参数指定密度,也就是每个条状图的占比例比,默认为1

color : 这个指定条状图的颜色

测试程序

import matplotlib.pyplot as plt
x = [1,1,2,3,3,3,4,4,4,4]
plt.hist(x)
plt.show()

横坐标为x中值的范围,纵坐标(y)为出现x中每个元素的个数

直方图-bar

## X为横轴坐标,y为柱高度
x = range(5)
y = [22,33,44,12,78]
plt.bar(x,height=y)
plt.show()


## 横向 
'''
orientation	是竖直条还是水平条	
height	条形的高度
width	线条的宽度
botton	条形的起始位置
'''
x = range(5)
y = [22,33,44,12,78]
plt.bar(x=0,bottom=x,width=y,height=0.5,orientation="horizontal")
plt.show()

在这里插入图片描述

饼图-pie

import matplotlib.pyplot as plt
# 参数1 类别数量,label 类别标签 ,explode 举例圆心距离 autopct 占比
plt.pie([10,10,10],labels=['1','2','3'],explode=(0.1,0,0),autopct='%1.1f%%')
plt.show()

箱线图-boxplot

import  numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.rand(10,5),columns=['A','B','C','D','E'])
f = df.boxplot(sym = 'o',            #异常点形状
               vert = True,          # 是否垂直
               whis=1.5,             # IQR
               patch_artist = True,  # 上下四分位框是否填充
               meanline = False,showmeans = True,  # 是否有均值线及其形状
               showbox = True,   # 是否显示箱线
               showfliers = True,  #是否显示异常值
               notch = False,    # 中间箱体是否缺口
               return_type='dict')  # 返回类型为字典
plt.title('箱线图')
plt.rcParams['font.sans-serif'] = 'SimHei'  # 设置中文显示
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值