改变 matplotlib histogram2d 的高度范围 [英] Altering height range of matplotlib histogram2d

查看:74
本文介绍了改变 matplotlib histogram2d 的高度范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 matplotlib 的 histogram2d 绘制一些二维经验概率分布.我希望颜色在几个不同的图中具有相同的比例,但即使我知道结果分布的全局上限和下限,也无法找到设置比例的方法.照原样,每个色标将从直方图bin的最小高度到最大高度,但是每个图的此范围将有所不同.

I am trying to plot some 2D empirical probability distributions using matplotlib's histogram2d. I want the colours to be on the same scale across several different plots, but cannot find a way to set a scale even if I know a global upper and lower bound on the resulting distribution. As is, each color scale will run from the minimum height to the maximum height of the histogram bins, but this range will be different for each plot.

一种可能的解决方案是强制一个 bin 占据我的下限的高度,另一个是我的上限.即使这似乎也不是一项非常简单的任务.

One potential solution would be to force one bin to take the height of my lower bound, and another my upper bound. Even this does not seem like a very straight forward task.

推荐答案

一般来说,matplotlib 中大部分东西的颜色缩放是由 vminvmax 关键字控制的参数.

In general, the color scaling of most things in matplotlib is controlled by the vmin and vmax keyword arguments.

您必须稍微阅读两行之间的内容,但正如文档中提到的,hist2d 中的其他 kwargs 会传递给 pcolorfast.因此,您可以通过 vminvmax kwargs 指定颜色限制.

You have to read between the lines a bit, but as the documentation mentions, additional kwargs in hist2d are passed on to pcolorfast. Therefore, you can specify the color limits through the vmin and vmax kwargs.

例如:

import numpy as np
import matplotlib.pyplot as plt

small_data = np.random.random((2, 10))
large_data = np.random.random((2, 100))

fig, axes = plt.subplots(ncols=2, figsize=(10, 5), sharex=True, sharey=True)

# For consistency's sake, we'll set the bins to be identical
bins = np.linspace(0, 1, 10)

axes[0].hist2d(*small_data, bins=bins, vmin=0, vmax=5)
axes[1].hist2d(*large_data, bins=bins, vmin=0, vmax=5)

plt.show()

这篇关于改变 matplotlib histogram2d 的高度范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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