子图中直方图的动画 [英] Animation of histograms in subplot

查看:93
本文介绍了子图中直方图的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下动画子图,它们模拟四种不同分布的直方图:

I have the following animated subplots that simulate histograms of four different distributions:

import numpy
from matplotlib.pylab import *
import matplotlib.animation as animation

n = 100

# generate 4 random variables from the random, gamma, exponential, and uniform distributions
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)

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

def updateData(curr):

    if curr == n: 
        a.event_source.stop()

    ax1.hist(x1[:curr], normed=True, bins=20, alpha=0.5)
    ax2.hist(x2[:curr], normed=True, bins=20, alpha=0.5)
    ax3.hist(x3[:curr], normed=True, bins=20, alpha=0.5)
    ax4.hist(x4[:curr], normed=True, bins=20, alpha=0.5)

simulation = animation.FuncAnimation(fig, updateData, interval=20, repeat=False)

plt.show()

它可以工作,但是由于某些原因,对于y轴缩放,normed = True被忽略.如果我从动画中删除这些图,它们将正确缩放.如何在动画中获得适当的缩放比例?

It works, but for some reason the normed=True is being ignored for the y-axis scaling. If I take these plots out of the animation, they scale properly. How do I get proper scaling in the animation?

EDIT

EDIT

不是像这样的缩放比例(动画之外):

Instead of having a scale like this (outside of animation):

我(在动画中)得到了:

I get (inside of animation):

推荐答案

直方图的normed = True参数使直方图绘制分布的密度.来自文档:

The normed = True argument to the histogram makes the histogram plot the density of the distribution. From the documentation:

标准:布尔值,可选
如果为True,则返回元组的第一个元素将是归一化以形成概率密度的计数,即n/(len(x)`dbin),即,直方图的积分总和为1 .如果stacked也为True,则直方图的总和将归一化为1. 默认值为False

normed : boolean, optional
If True, the first element of the return tuple will be the counts normalized to form a probability density, i.e., n/(len(x)`dbin), i.e., the integral of the histogram will sum to 1. If stacked is also True, the sum of the histograms is normalized to 1. Default is False

这意味着直方图条的高度取决于容器宽度.如果像在动画开始时那样仅绘制一个数据点,则条形高度将为1./binwidth.如果纸槽宽度小于零,则条形高度可能会变得非常大.

This means that the hight of the histogram bar depends on the bin width. If only one data point is plotted as is the case at the beginning of the animation the bar height will be 1./binwidth. If the bin width is smaller than zero, the bar height might become very large.

因此,最好固定箱并在整个动画中使用它们.
清除轴以免绘制100个不同的直方图也是合理的.

It's therefore a good idea to fix the bins and use them throughout the animation.
It's also reasonable to clear the axes such that there are not 100 different histograms being plotted.

import numpy as np
from matplotlib.pylab import *
import matplotlib.animation as animation

# generate 4 random variables from the random, gamma, exponential, and uniform distribution
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)

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

def updateData(curr):
    if curr <=2: return
    for ax in (ax1, ax2, ax3, ax4):
        ax.clear()
    ax1.hist(x1[:curr], normed=True, bins=np.linspace(-6,1, num=21), alpha=0.5)
    ax2.hist(x2[:curr], normed=True, bins=np.linspace(0,15,num=21), alpha=0.5)
    ax3.hist(x3[:curr], normed=True, bins=np.linspace(7,20,num=21), alpha=0.5)
    ax4.hist(x4[:curr], normed=True, bins=np.linspace(14,20,num=21), alpha=0.5)

simulation = animation.FuncAnimation(fig, updateData, interval=50, repeat=False)

plt.show()

这篇关于子图中直方图的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆