gpt4 book ai didi

python - 如何修改 hexbin 图的面色?

转载 作者:行者123 更新时间:2023-12-05 05:31:17 27 4
gpt4 key购买 nike

我正在寻找一种方法来微调 hexbin 图中单个细胞的颜色。

我尝试使用来自 PolyCollection 的方法 set_facecolors改变单个单元格的颜色,但这似乎不起作用。

示例:这应该会导致 hexbin 的中心有一个红色单元格:

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

知道如何设置单个单元格的颜色吗?

最佳答案

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

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

关于python - 如何修改 hexbin 图的面色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74405187/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com