Matplotlib http://matplotlib.sourceforge.net/users/image_tutorial.html有imshow
绘制数组的方法:
import matplotlib as mpl
from matplotlib import pyplot
import numpy as np
# make values from -5 to 5, for this example
zvals = np.random.rand(100,100)*10-5
# make a color map of fixed colors
cmap = mpl.colors.ListedColormap(['blue','black','red'])
bounds=[-6,-2,2,6]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
# tell imshow about color map so that only set colors are used
img = pyplot.imshow(zvals,interpolation='nearest',
cmap = cmap,norm=norm)
# make a color bar
pyplot.colorbar(img,cmap=cmap,
norm=norm,boundaries=bounds,ticks=[-5,0,5])
pyplot.show()
它看起来是这样的:
颜色条设置的详细信息取自 matplotlib 示例:colorbar_only.py。 http://matplotlib.sourceforge.net/examples/api/colorbar_only.html它解释了数量boundaries
需要比颜色数大一。
EDIT
你应该note http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow, that imshow
接受origin
关键字,设置第一个点的分配位置。默认值为“左上角”,这就是为什么在我发布的图中,y 轴在左上角为 0,在左下角为 99(未显示)。另一种方法是设置origin="lower"
,以便第一个点绘制在左下角。
EDIT 2
如果您想要渐变而不是离散颜色图,请通过以下方式制作颜色图线性插值 http://matplotlib.sourceforge.net/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap.from_list通过一系列的颜色:
fig = pyplot.figure(2)
cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
['blue','black','red'],
256)
img2 = pyplot.imshow(zvals,interpolation='nearest',
cmap = cmap2,
origin='lower')
pyplot.colorbar(img2,cmap=cmap2)
fig.savefig("image2.png")
This produces:
![enter image description here](https://i.stack.imgur.com/bCttt.png)
EDIT 3
添加网格,如下所示example http://matplotlib.sourceforge.net/examples/pylab_examples/axes_props.html, 使用grid
方法。将网格颜色设置为“白色”与颜色图使用的颜色配合良好(即默认的黑色显示效果不佳)。
pyplot.grid(True,color='white')
Including this before the savefig
call produces this plot (made using 11x11 grid for clarity):
There are many options for grid
, which are described in the matplotlib documentation http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.grid. One you might be interested in is linewidth
.