Skip to content

Instantly share code, notes, and snippets.

@YimianDai
Last active September 19, 2019 22:23
Show Gist options
  • Save YimianDai/380c02d6d3ca30091084e199e1759fc7 to your computer and use it in GitHub Desktop.
Save YimianDai/380c02d6d3ca30091084e199e1759fc7 to your computer and use it in GitHub Desktop.
Notes on Matplotlib

Matplotlib 3.1 Cheatsheet

生成并保存不含有 margin 的图像

下面的设置是我找到的 best practice 了。

        plt.gca().set_axis_off()
        plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
        plt.margins(0, 0)
        plt.gca().xaxis.set_major_locator(plt.NullLocator())
        plt.gca().yaxis.set_major_locator(plt.NullLocator())
        output_name = os.path.join(outdir, img_id+"_detection.png")
        plt.savefig(output_name, bbox_inches='tight', transparent=True,
                    pad_inches=0)
        plt.clf()

要记得关掉画图 figure 或者 axis 否则会报 warning about too many open figures,参见 https://stackoverflow.com/questions/21884271/warning-about-too-many-open-figures

Plot

Plot Heatmap

heat_map should be numpy array

plt.imshow(heat_map)
plt.colorbar()
plt.show()
Plot Grayscale Image
# img must be numpy array, not mxnet.nd
plt.imshow(img, cmap='gray')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment