控制Matplotlib中的颜色矩阵图范围

2024-07-08 15:47:29 发布

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

我想在matplotlib中绘制一个类似下图的矩阵图。我可以用下面的代码来绘制这样的图:

m = numpy.random.rand(100,100)
matplotlib.pyplot.matshow(m)

如何控制色阶,即设置与“最小”和“最大”颜色对应的值?


Tags: 代码numpymatplotlib颜色绘制矩阵randompyplot
1条回答
网友
1楼 · 发布于 2024-07-08 15:47:29

matshow文档表明,这些选项大多只是传递给imshowdocs)。Imshow采用参数vminvmax,这些参数根据您的需要确定最小和最大颜色。我们来看一个例子:

import numpy as np
import matplotlib.pyplot as plt
plt.ion()
A = np.arange(0,100).reshape(10,10)
plt.matshow(A)                        # defaults
plt.matshow(A, vmin=0, vmax=99)       # same
plt.matshow(A, vmin=10, vmax=90)      # top/bottom rows get min/max colors, respectively

旁白:

我也可以建议您更改颜色图吗?例如cmap='hot'。尽管这是默认的(为什么?),则'jet'颜色映射为almost never the best choice

x = np.random.randn(1000)
y = np.random.randn(1000)+5
plt.hist2d(x, y, bins=40, cmap='hot')
plt.colorbar()

output of the examle code with cmap='hot'

相关问题 更多 >

    热门问题