加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
matploatlib.5.py 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
import matplotlib.pyplot as plt
import random
import matplotlib
# 设置matplotlib支持中文
font = {'family' : 'SimHei',
'weight' : 'bold',
'size' : '10'}
matplotlib.rc("font" , **font)
#####################################################################
#https://www.runoob.com/matplotlib/matplotlib-hist.html
#############################没有统计过的数据可以用hist来绘制直方图################################
# x=[]
# for i in range(100):
# x.append(random.randint(100, 150))
#或者这样写
x = [random.randint(50, 150) for _ in range(100)]
#组距与组数
d=10
num_bins=(max(x)-min(x))//d
print(max(x),min(x),max(x)-min(x),num_bins)
#设置图片大小,dpi越大越清晰
plt.figure(figsize=(10,8),dpi=80)
# 分布直方图,density表示是否是否将直方图归一化
plt.hist(x,num_bins,color="red",density=True)
# 取补步长,数字与字符串一一对应 ,rotation为旋转的度数
plt.xticks(range(min(x),max(x)+d,d),rotation=45)
#添加网格,alpha为透明度
plt.grid(alpha=0.4)
#添加描述信息
plt.xlabel("数字")
plt.ylabel("个数")
plt.title("变化")
#显示图片
plt.show()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化