Matplotlib,我正在做一个2d直方图,我想做一个三角形轴扩展,类似于用色条做的

2024-05-13 03:38:40 发布

您现在位置:Python中文网/ 问答频道 /正文

使用MATPLOTLIB.PYPLOT文件我正在做一个二维直方图,我想把我的视野限制在一个特定的y值范围内,但同时我想把y轴底部某个数字以下的所有值都聚集起来。我想的是类似于你如何使一个色条有三角形的延伸,在这些延伸内的任何东西都是在一定的条件下绘制的。我想做的是类似的,除了涉及y轴。你知道吗


Tags: 文件matplotlib绘制数字直方图条件pyplot三角形
1条回答
网友
1楼 · 发布于 2024-05-13 03:38:40

我能想到的最简单的方法就是在调用直方图之前过滤数据。下面是一个使用np.where过滤y数据的示例。你知道吗

import numpy as np; np.random.seed(17)
import matplotlib.pyplot as plt

# generate some random normal data
N = 100000
x = np.random.normal(-2, 3, N) 
y = np.random.normal(3, 6, N) 

# use np.where to set all y values less than -10 to -10
y = np.where(y < -10, -10, y)

# now apply histogram2d
xbins = np.linspace(-10, 10, 101)
ybins = np.linspace(-10, 10, 101)

H, xedges, yedges = np.histogram2d(y, x, bins=(xbins, ybins))
X, Y = np.meshgrid(xedges, yedges)

# plot data with pcolormesh
fig = plt.figure()
ax = fig.add_subplot(111)

img = ax.pcolormesh(X, Y, H)

ax.set_xlim(xedges.min(), xedges.max())
ax.set_ylim(yedges.min(), yedges.max())

plt.colorbar(img)

可以在y轴底部看到所有阈值的分布。你知道吗

enter image description here

相关问题 更多 >