【DS实践 | Coursera】Practice Assignment | Applied Plotting, Charting & Data Representation in Python


一、问题分析

1.1 问题描述

Understanding Distributions Through Sampling

To complete this assignment, create a code cell that:

  • Creates a number of subplots using the pyplot subplots or matplotlib gridspec functionality.
  • Creates an animation, pulling between 100 and 1000 samples from each of the random variables (x1, x2, x3, x4) for each plot and plotting this as we did in the lecture on animation.
  • Bonus: looking into matplotlib widgets and adding a widget which allows for parameterization of the distributions behind the sampling animations.

Tips:

  • Before you start, think about the different ways you can create this visualization to be as interesting and effective as possible.
  • Take a look at the histograms below to get an idea of what the random variables look like, as well as their positioning with respect to one another. This is just a guide, so be creative in how you lay things out!
  • Try to keep the length of your animation reasonable (roughly between 10 and 30 seconds).

1.2 问题分析

  将直方图的生成过程可视化。

二、代码详情

2.1 代码

import matplotlib.pyplot as plt
import numpy as np

%matplotlib notebook

#生成数据
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)

#做直方图
plt.figure(figsize=(9,3))
plt.hist(x1, density=True, bins=20, alpha=0.5)
plt.hist(x2, density=True, bins=20, alpha=0.5)
plt.hist(x3, density=True, bins=20, alpha=0.5)
plt.hist(x4, density=True, bins=20, alpha=0.5);
plt.axis([-7,21,0,0.6])

#添加备注
plt.text(x1.mean()-1.5, 0.5, 'x1\nNormal')
plt.text(x2.mean()-1.5, 0.5, 'x2\nGamma')
plt.text(x3.mean()-1.5, 0.5, 'x3\nExponential')
plt.text(x4.mean()-1.5, 0.5, 'x4\nUniform')

在这里插入图片描述

import matplotlib.animation as animation
import numpy as np
import matplotlib.pyplot as plt


n1=10
n2=1000

x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)

#动画函数
def update(curr):
    
    #到1000片的时候停止
    if curr == n2:
        a.event_source.stop()
        
    #对正态分布图像绘制直方图
    plt.sca(axes[0,0])
    plt.cla()
    plt.hist(x1[:curr], density=True, bins=np.arange(-7, 1, 0.5), alpha=0.5)
    plt.gca().set_title('Normal')
    plt.axis([-7,1,0,0.5])
    
	#对Gamma分布图像绘制直方图
    plt.sca(axes[0,1])
    plt.cla()
    plt.hist(x2[:curr], density=True, bins=np.arange(-1, 15, 0.5), alpha=0.5)
    plt.gca().set_title('Gamma')
    plt.axis([-1,15,0,0.5])

	#对指数分布图像绘制直方图
    plt.sca(axes[1,0])
    plt.cla()
    plt.hist(x3[:curr], density=True, bins=np.arange(6, 25, 0.5), alpha=0.5)
    plt.gca().set_title('Exponential')
    plt.axis([6,25,0,0.5])

	#对均匀分布图像绘制直方图
    plt.sca(axes[1,1])
    plt.cla()
    plt.hist(x4[:curr], density=True, bins=np.arange(13, 21, 0.5), alpha=0.5)
    plt.gca().set_title('Uniform')
    plt.axis([13,21,0,0.5])

fig,axes=plt.subplots(2,2,figsize=(8,8),sharey=True)
a=animation.FuncAnimation(fig,update,interval=100)

2.2 结果

  得到的结果应该是动画的形式,这里仅截两张图:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值