我有一个非常大的2d形状数组(186295,2),每个2元素子数组的第一个元素是x,第二个元素是y。这是我通过将x和y分量分开来生成散点图的方式matplotlib
:
ax.scatter(A[:, 0]+np.random.uniform(-.02, .02, A.shape[0]), A[:, 1], s=2, color='b', alpha=0.5, zorder=3)
但是,我想
x值在[8,9.2]范围内的所有点都以点图的形式显示在中点x = 8.6处,
x值在[9.2,10.4]范围内的所有点均以点图形式显示在中点x = 9.8处,
x值在[10.4,12.2]范围内的所有点均以点图的形式显示在中点x = 11.3处。
非常感谢您的帮助,
您可以使用np.select
:
例:
import numpy as np
from matplotlib import pyplot as plt
n=100
x = np.random.uniform(8, 12, n)
y = np.random.uniform(.01, 1, n)
a = np.array(list(zip(x,y)))
fig,ax = plt.subplots(2, sharex=True)
ax[0].scatter(a[:,0], a[:,1])
ax[0].title.set_text('Scatter Plot')
conditions = [a[:,0]<=8, a[:,0]<=9.2, a[:,0]<=10.4, a[:,0]<=12.2, a[:,0]>12.2]
choices = [a[:,0], 8.6, 9.8, 11.3, a[:,0]]
a[:,0] = np.select(conditions, choices)
ax[1].scatter(a[:,0], a[:,1])
ax[1].title.set_text('Dot Plot')
结果:
另一种可能性是使用np.digitize
它,因为它使用bin(上界)列表而不是条件列表,从而节省了键入内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句