[英]How do you select a custom bin size for a 2D histogram?
I already know how to set the bin size for a 1D histogram after looking at this answer ,but I don't know how to do this for a 2D histogram. 我已经知道如何在查看此答案后设置1D直方图的bin大小,但我不知道如何为2D直方图执行此操作。 I have referred to both the Numpy and Matplotlib pages for the 2D histograms, but all my attempts to adjust the bin size did not work/run.I want my bins to have a length of 20 and a width of 2. 我已经提到了Numpy和Matplotlib页面的2D直方图,但是我调整bin大小的所有尝试都没有工作/运行。我希望我的箱子的长度为20,宽度为2。
Example code: 示例代码:
import numpy as np
import matplotlib.pyplot as plt
import math
dataX = np.random.uniform(-50,50,100)
dataY = np.random.uniform(-50,50,100)
binWidth = 2.0
binLength = 20.0
xMin = min(dataX)
xMax = max(dataX)
yMin = min(dataY)
yMax = max(dataY)
#this and all other similar attempts that I have tried did not run
np.histogram2d(dataX, dataY, bins = np.arange((xMin,xMax + binWidth),(yMin,yMin +binLength),(binWidth,binLength)))
Looking forward to your hints/help/advice. 期待您的提示/帮助/建议。
You could do eg 你可以这样做
plt.hist2d(dataX, dataY, bins = [np.arange(xMin, xMax, binWidth), np.arange(yMin, yMax, binLength)])
bins accepts integers or arrays for defining the number of bins or their edges, each of it either once for both dimensions or in a list with two entries to give two different definietions for x and y. bins接受整数或数组来定义bin或其边缘的数量,每个bin或者两个维度都是一次,或者在包含两个条目的列表中为x和y提供两个不同的定义。
From the docs: 来自文档:
bins : int or array_like or [int, int] or [array, array], optional bin:int或array_like或[int,int]或[array,array],可选
The bin specification: 垃圾箱规格:
If int, the number of bins for the two dimensions (nx=ny=bins). If array_like, the bin edges for the two dimensions (x_edges=y_edges=bins). If [int, int], the number of bins in each dimension (nx, ny = bins). If [array, array], the bin edges in each dimension (x_edges, y_edges = bins). A combination [int, array] or [array, int], where int is the number of bins and array is the bin edges.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.