matlab imshow truesize,matplotlib equivalent for MATLABs truesize()

问题

I am new to matplotlib and python and would like to display an image so that 1 pixel of the image is actually represented by 1 pixel in the figure. In MATLAB, this is achieved with the command truesize(). How can I do this in Python?

I tried playing around with the imshow() arguments as well as set_dpi() and set_figwidth()/set_figheight(), but with no luck.

Thanks.

回答1:

If you want to create images right down to the pixel level, why not use PIL in the first place? That way you wouldn't have to programatically calculate your true drawing area by substracting margins, labels and axis widths from the figure extend.

回答2:

This hack does what I wanted to do, though it's still not perfect:

h = mplt.imshow(img, interpolation='nearest')

dpi = h.figure.get_dpi()

h.figure.set_figwidth(img.shape[0] / dpi)

h.figure.set_figheight(img.shape[1] / dpi)

h.figure.canvas.resize(img.shape[1] + 1, img.shape[0] + 1)

h.axes.set_position([0, 0, 1, 1])

h.axes.set_xlim(-1, img.shape[1])

h.axes.set_ylim(img.shape[0], -1)

It can be generalized to account for a margin around the axes holding the image.

来源:https://stackoverflow.com/questions/2645049/matplotlib-equivalent-for-matlabs-truesize

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值