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

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

问题描述

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

导入numpy从 matplotlib.pylab 导入 *导入 matplotlib.animation 作为动画n = 100# 从随机分布、伽马分布、指数分布和均匀分布中生成 4 个随机变量x1 = np.random.normal(-2.5, 1, 10000)x2 = np.random.gamma(2, 1.5, 10000)x3 = np.random.exponential(2, 10000)+7x4 = np.random.uniform(14,20, 10000)图, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)定义更新数据(当前):如果 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)模拟=animation.FuncAnimation(无花果,更新数据,间隔=20,重复=假)plt.show()

它有效,但由于某种原因,y 轴缩放忽略了 normed=True.如果我从动画中取出这些图,它们会正确缩放.如何在动画中正确缩放?

编辑

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

我得到(动画内部):

解决方案

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

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()

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

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

I get (inside of animation):

解决方案

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

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

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.

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天全站免登陆