使用subplot时删除白色边框并在python中显示imshow(Matplotlib)

时间:2016-06-14 10:41:03

标签: python matplotlib figure imshow

    at org.python.core.Py.TypeError(Py.java:235)
    at org.python.core.PyObject.__finditem__(PyObject.java:585)
    at org.python.core.PyObjectDerived.__finditem__(PyObjectDerived.java:861)
    at org.python.core.PyObject.__getitem__(PyObject.java:653)
    at org.python.core.PyObjectDerived.__getitem__(PyObjectDerived.java:901)
    at org.python.core.PyObject.__getslice__(PyObject.java:740)
    at org.python.core.PyObjectDerived.__getslice__(PyObjectDerived.java:924)
    at org.python.pycode._pyx3.agekeyed$1(keying.py:6)
    at org.python.pycode._pyx3.call_function(keying.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
    at org.python.core.PyFunction.function___call__(PyFunction.java:376)
    at org.python.core.PyFunction.__call__(PyFunction.java:371)
    at org.python.core.PyFunction.__call__(PyFunction.java:361)
    at org.python.core.PyFunction.__call__(PyFunction.java:356)
    at org.apache.pig.scripting.jython.JythonFunction.exec(JythonFunction.java:117)
    ... 16 more

我使用以下代码保存图像

import numpy as np
import sys
import matplotlib as mpl
import matplotlib.pyplot as plt

然而,我得到的是 enter image description here 而这显然仍然有一个白色的边框。 我该如何摆脱它?

array.shape是:(256,256,1,1,3)

2 个答案:

答案 0 :(得分:10)

看看我的例子,它可以帮到你:

import numpy as np
import matplotlib.pyplot as plt

def save_image(data, filename):
    sizes = np.shape(data)     
    fig = plt.figure()
    fig.set_size_inches(1. * sizes[0] / sizes[1], 1, forward = False)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)
    ax.imshow(data)
    plt.savefig(filename, dpi = sizes[0], cmap='hot') 
    plt.close()

data = np.random.randint(0, 100, (256, 256))
save_image(data, '1.png')

enter image description here

答案 1 :(得分:1)

对上述答案的修改很少:

def save_image(data, filename):
  sizes = np.shape(data)     
  fig = plt.figure(figsize=(1,1))
  ax = plt.Axes(fig, [0., 0., 1., 1.])
  ax.set_axis_off()
  fig.add_axes(ax)
  ax.imshow(data, cmap = plt.get_cmap("bone"))
  plt.savefig(filename, dpi = sizes[0]) 
  plt.close()