matplotlib画图(三)——直方图

直方图

直方图是用来展现连续型数据分布特征的统计图形。利用直方图我们口语i直观地分析出数据的集中趋势和波动情况。

普通直方图

最近放开三胎了, 我们以这个作为一个因子, 就大家对生几个孩子的意愿情况作一个直方图:

import random
# 配置中文显示不乱码
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
bins = range(0, 4, 1)
x = [random.randint(0, 3) for i in range(1000)]

plt.hist(x=x, bins=bins, histtype='bar', rwidth=1.0, alpha=0.6, label="关于生孩子数量意愿统计表", color="#377eb8")
# 图标标题位置设置
plt.legend(loc="best")

# 横纵坐标轴名称设置
plt.xlabel("生孩子数量")
plt.ylabel("人数")
plt.grid()
plt.show()

上述结果为:
在这里插入图片描述

堆积直方图

假设现在要查看下不同性别的情况下, 针对生孩子个数意愿情况做统计:

import random
# 配置中文显示不乱码
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
bins = range(0, 4, 1)
x_man = [random.randint(0, 3) + random.choice([0, 1]) for i in range(1000)]
x_woman = [random.randint(0, 3) + random.choice([0, 1]) for i in range(1000)]
x = [x_man, x_woman]

color = ["#377eb8", "#bebada"]

plt.hist(x=x, bins=bins, histtype='bar', rwidth=1.0, alpha=0.6, label="关于生孩子数量意愿统计表", color=color, stacked=True)
# 图标标题位置设置
plt.legend(loc="best")

# 横纵坐标轴名称设置
plt.xlabel("生孩子数量")
plt.ylabel("人数")
plt.grid()
plt.show()

上述结果为:
p2

并排直方图

还是上述的情形,统计男性与女性对生孩子个数的意愿。

import random
# 配置中文显示不乱码
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
bins = range(0, 4, 1)
x_man = [random.randint(0, 3) + random.choice([0, 1]) for i in range(1000)]
x_woman = [random.randint(0, 3) + random.choice([0, 1]) for i in range(1000)]
x = [x_man, x_woman]

colors = ["#377eb8", "#bebada"]
labels = ["男性", "女性"]

plt.hist(x=x, bins=bins, histtype='bar', rwidth=1.0, alpha=0.6, label=labels, color=colors, stacked=False)

# hist方法的 label 标签位置布局等设置
plt.legend(loc="upper left", bbox_to_anchor=(0.1, 0.9), shadow=True, fancybox=True)

# 横纵坐标轴名称设置
plt.xlabel("生孩子数量")
plt.ylabel("人数")

# 设置横坐标最小刻度范围
plt.xlim(0)

plt.title("关于生孩子数量意愿统计表")

# 绘制栅格
plt.grid()
plt.show()

上述结果如下:
p5

上述横坐标的 0 并不是横纵坐标的交汇处, 可通过 plt.xlim(0)设置横坐标的最小数据范围,以达到下图的效果:
p4

重点方法与参数

# 配置中文显示不乱码
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False

# hist方法的 label 标签位置布局设置
plt.legend(loc="upper left")

# 横纵坐标轴名称设置
plt.xlabel("生孩子数量")
plt.ylabel("人数")

# 统计图内部增加栅格
plt.grid()

hist 直方图主要参数

plt.hist(x=x, bins=bins, histtype='bar', rwidth=1.0, alpha=0.6, label="关于生孩子数量意愿统计表", color=color, stacked=False)
参数解释
x连续型数据输入值
bins用于确定主题的个数或是主题边缘范围;除了最后一个柱体的数据范围是闭区间,其他柱体的数据范围都是左闭右开区间。
color柱体的颜色
histtype柱体类型
label图例内容
rwidth柱体的相对宽度,取值范围是[0.0, 1.0]

lengend() 方法主要参数

ptl.legend(loc='upper left', bbox_to_anchor=(0.05, 0.95), ncol=3, title="power function", shadow=True, fancybox=True)
参数解释
loc位置参数,主要有:upper right, upper left, lower left, lower right, center left, center right, lower ceneter, upper center, center;
bbox_to_anchor线框位置参数;参数是一个四元元组, 且使用Axes坐标系统。第一个元素代表距离画布左侧的x轴长度的倍数的距离;第二个元素代表距离画布底部的y轴长度的倍数的距离;第三个元素代表x轴长度的倍数的线框长度;第四个元素代表y轴长度的倍数的线框宽度。当x轴长度为2, y轴长度为8的时候,legend(loc=‘upper left’, bbox_to_anchor=(0.05, 0.95))会将图例放在上方左手边拐角处的距离坐标轴左边 20.05=0.1, 底部 80.95=7.6的位置。
title图例标签内容的标题参数
shadow控制线框是否添加阴影的参数(boolean 类型)
fancybox控制线框是否圆角/直角的参数(boolean类型)

title()方法主要参数

plt.title(label="关于生孩子数量意愿统计表", loc="center", size=20, style="oblique", color="c")
参数解释
label标题名称参数
loc标题展示位置参数, 有: left, center, right;
family字体类别参数,如需设置,需有对应字体库
size字体大小参数
color字体颜色参数
style字体风格参数

设置坐标轴刻度范围

    # 设置坐标最小/最大刻度范围
    plt.xlim(xmin=0)
    plt.ylim(ymin=100, ymax=600)

设置坐标刻度标签

    # 设置刻度标签
    plt.xticks(ticks=[0, 1, 2, 3])
    plt.yticks(ticks=[100, 300, 500], labels=["100", "100*3", "100*5"])
参数解释
ticks坐标轴上需要标注的刻度
labels基于ticks,需要标注的刻度显示的数据, 若无指定,则展示实际刻度值

来一个参数完整版的看看张什么样?

import random
import matplotlib as mpl
import matplotlib.pyplot as plt

# 配置中文显示不乱码
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
bins = range(0, 4, 1)
x_man = [random.randint(0, 3) + random.choice([0, 1]) for i in range(1000)]
x_woman = [random.randint(0, 3) + random.choice([0, 1]) for i in range(1000)]
x = [x_man, x_woman]

colors = ["#377eb8", "#bebada"]
labels = ["男性", "女性"]

plt.hist(x=x, bins=bins, histtype='bar', rwidth=1.0, alpha=0.6, label=labels, color=colors, stacked=False)

# hist方法的 label 标签位置布局等设置
plt.legend(loc="upper left", bbox_to_anchor=(0.1, 0.9), shadow=True, fancybox=True)

# 横纵坐标轴名称设置
plt.xlabel("生孩子数量")
plt.ylabel("人数")

# 设置坐标最小/最大刻度范围
plt.xlim(xmin=0)
plt.ylim(ymin=100, ymax=600)

# 设置刻度标签
plt.xticks(ticks=[0, 1, 2, 3])
plt.yticks(ticks=[100, 300, 500], labels=["100", "100*3", "100*5"])

# 设置标题
plt.title(label="关于生孩子数量意愿统计表", loc="center", size=20, style="oblique", color="c")

# 绘制栅格
plt.grid()
plt.show()

代码执行结果如下:
P6

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值