使用matplotlib颜色映射和稀疏矩阵

2024-06-01 13:30:23 发布

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

我试图用matplotlib.pylabscipy.sparse绘制一个稀疏矩阵,但是我希望每一行从渐变颜色贴图(现在,我正在考虑使用“jet”colormap)采用不同的颜色(不一定以固定的间隔)。在渐变色列表中选择渐变值的行。我想我需要以某种方式使用matplotlib.pyplot.imshow(),但我不确定。在

以下是我目前掌握的一些代码:

import numpy as np
import matplotlib
import matplotlib.pylab as plt
import scipy.sparse as sps

m = sps.rand(16,16, density=0.25)
row = m.row
col = m.col
# gradient indexes into the colormap
gradient = np.linspace(0, 1, 16)
# Plot the sparse matrix.
# TODO: Use a colormap in pylab.spy() or elsewhere?
plt.spy(m, markersize=5)
plt.show()

Tags: theimportmatplotlib颜色asnppltcol