作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种方法来微调 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()
知道如何设置单个单元格的颜色吗?
最佳答案
通常,颜色是通过颜色图设置的(因此 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()
关于python - 如何修改 hexbin 图的面色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74405187/
我是一名优秀的程序员,十分优秀!