如何修改六边形散点图的面颜色?

3
我正在寻找一种方法来微调六边形图中单个单元格的颜色。
我尝试使用PolyCollection中的set_facecolors方法来改变单个单元格的颜色,但似乎无效。
例如:这应该会在中心产生一个红色的六边形图。
import numpy as np
import matplotlib.pyplot as plt

x = y = np.linspace(0.,1.,10)
pts=np.vstack(np.meshgrid(x,y,indexing='ij')).reshape(2,-1).T
z = np.zeros_like(pts[:,0])

fig, ax = plt.subplots(figsize=(3, 3))
hb = ax.hexbin(pts[:,0],pts[:,1], C=z, gridsize=(5,3), vmin=-1., edgecolors='white')
ax.set_xlim([0,1])
ax.set_ylim([0,1])

ax.figure.canvas.draw()
fcolors = hb.get_facecolors()
fcolors[31] = [1., 0., 0., 1.]
hb.set_facecolors(fcolors)

plt.show()

Hexbin plot should feature a red cell in its center

有没有关于如何设置单元格面颜色的想法?

1个回答

1

通常,颜色是通过一个颜色映射来设置的(因此在ax.hexbin()中有C=z参数)。要更改单个颜色,您需要禁用该行为。这可以通过将PolyCollection的“数组”设置为None来实现(请参见Joe Kington在这里的答案)。

import matplotlib.pyplot as plt
import numpy as np;

x = y = np.linspace(0., 1., 10)
pts = np.vstack(np.meshgrid(x, y, indexing='ij')).reshape(2, -1).T
z = np.zeros_like(pts[:, 0])

fig, ax = plt.subplots(figsize=(3, 3))
hb = ax.hexbin(pts[:, 0], pts[:, 1], C=z, gridsize=(5, 3), vmin=-1., edgecolors='white')
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])

ax.figure.canvas.draw()

fcolors = hb.get_facecolors()
fcolors[31] = [1., 0., 0., 1.]
hb.set(array=None, facecolors=fcolors)

plt.show()

changing individual colors in a hexbin


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接