简体   繁体   中英

Rotate an image in matplotlib?

I want to rotate an image generated by plt.imshow() . Is that possible somehow? I don't want to rotate (with scipy.ndimage.rotate for example) the underlying array I use for plotting, because it always introduces artifacts, and they are affecting what I'm looking at in the image. Also would be better for me to have pixels rotated by 45 degrees.

I've found this , where it's suggested to use use the scipy rotate method on the figure. But it doesn't work for me. This is the relevant code:

im = plt.imshow(np.log2(data+1), origin='lower', interpolation='none', cmap=WhRd,
                        extent=[0, (end-start)*200000, 0, 50*200000])
im_rot = ndimage.rotate(im, 45)

And this is the error, pointing at the second line:

/usr/local/lib/python2.7/dist-packages/scipy/ndimage/interpolation.pyc in rotate(input, angle, axes, reshape, output, order, mode, cval, prefilter)
    631         axes[1] += rank
    632     if axes[0] < 0 or axes[1] < 0 or axes[0] > rank or axes[1] > rank:
--> 633         raise RuntimeError('invalid rotation plane specified')
    634     if axes[0] > axes[1]:
    635         axes = axes[1], axes[0]

RuntimeError: invalid rotation plane specified

Is there any way to achieve what I want?

Two suggestions:

  • Maybe using ndimage.rotate with interpolation would reduce the artifacts in your plot? You can select the interpolation method which fits you requirements.

  • Maybe this post might be of help - it doesn't use ndimage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM