Matplotlib imshow colormap not covering full dynamic range of data

泪湿孤枕 提交于 2020-06-27 16:39:11

问题


I'm trying to produce colormaps of numpy arrays with large dynamic ranges. To that end, I've tried using imshow from matplotlib.pyplot, but there appears to be a problem with the colormap.

Specifically, every value in the array below a certain threshold is plotted as the same color as the minimum value in the array.

Below is a minimal working example. I produce an array containing values from 1e-4 to 1e+20, but in imshow all cells containing values less than 1e+3 appear to be set equal to 1e-4. However, there is no such issue using pcolormesh.

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np

# 2d (5x5) array with large dynamic range: 1e-4, 1e-3, ..., 1e+20
a = np.logspace(-4, 20, num=25).reshape(5, 5)

# imshow producing wrong image
fig = plt.figure()
plt.imshow(a, norm=LogNorm(), origin='lower')
plt.colorbar()
fig.savefig("imshow_broken.png")

# pcolormesh producing correct image
fig = plt.figure()
plt.pcolormesh(a, norm=LogNorm())
plt.colorbar()
fig.savefig("pcolormesh_working.png")

I'm using matplotlib version 2.1.2, but the problem seems to disappear on older versions, e.g. 1.5.3, perhaps suggesting a bug in recent matplotlib versions.

imshow:

pcolormesh:

来源:https://stackoverflow.com/questions/50879082/matplotlib-imshow-colormap-not-covering-full-dynamic-range-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!