我想生成一个坐标已旋转的网格。我必须在双循环中进行旋转,并且我确信有更好的方法来对其进行矢量化。代码如下:
# Define the range for x and y in the unrotated matrix
xspan = linspace(-2*pi, 2*pi, 101)
yspan = linspace(-2*pi, 2*pi, 101)
# Generate a meshgrid and rotate it by RotRad radians.
def DoRotation(xspan, yspan, RotRad=0):
# Clockwise, 2D rotation matrix
RotMatrix = np.array([ [np.cos(RotRad), np.sin(RotRad)],
[-np.sin(RotRad), np.cos(RotRad)]])
print RotMatrix
# This makes two 2D arrays which are the x and y coordinates for each point.
x, y = meshgrid(xspan,yspan)
# After rotating, I'll have another two 2D arrays …Run Code Online (Sandbox Code Playgroud) 我有一个散点图,它从两个不同的数据集中绘制大量的点。在某些区域,存在大量的点,因此即使具有非常低的 alpha(例如 alpha=0.1),您也无法看穿质量。但在该 alpha 值下,您几乎看不到稀疏区域中的点。有没有一种方法可以限制堆叠点的 alpha 值,或者以某种方式使背景在密集区域下可见,同时不洗掉稀疏区域?
代码片段如下所示:
# Code to populate the datasets not included.
fig, ax = plt.subplots()
ax.scatter(x1, y1, s=12, color='red')
ax.scatter(x2, y2, s=12, color='blue', alpha=0.1)
# Plus code to do xlabels and such not included.
Run Code Online (Sandbox Code Playgroud)
产生这个:

正如您所看到的,很难看到底部红色腿的边界,但仍然使顶部蓝色腿打出。
有什么办法可以创造出这样的效果吗?
提前致谢。
编辑
一个好的建议似乎是使用 hexbin 而不是 scatter。这看起来确实很有希望,但颜色仍然不能很好地混合。例如,
ax.hexbin(x1, y1, cmap='Reds', mincnt=1, vmax=100)
ax.hexbin(x2, y2, cmap='Blues', mincnt=1, vmax=50, alpha=0.8, linewidths=0)
Run Code Online (Sandbox Code Playgroud)
产量:

如果能让蓝色和红色融合在一起,那就太好了。也许每个像素可以有一个来自一个数据集的 R 值,以及来自另一个数据集的 B 值或其他值?但在 hexbin 中似乎不是一个选项。
编辑
应用托马西洛的答案后:
谢谢,我觉得比原版好看。