matplotlib plot rotation 90 degree is not happenin

2019-08-09 14:34发布

I am finding the edges of the images using matplotlib.I have done almost.And i want to rotate the image as 90 degree in plot.But it is not working for me.I have tried many things.Below is my code what i have tried.

from scipy import misc
from skimage import color,measure
import matplotlib.pyplot as plt
from skimage.draw import ellipse
from skimage.measure import find_contours, approximate_polygon, subdivide_polygon
from PIL import Image
import numpy as np

filename = r"images/5601.jpg"
fimg = misc.imread(filename)
gimg = color.colorconv.rgb2grey(fimg)
contours = measure.find_contours(gimg, 0.8)
for n, contour in enumerate(contours):
    plt.plot(contour[:, 1], contour[:, 0], linewidth=2)

contour = contours[0]
new_s = contour.copy()
appr_s = approximate_polygon(new_s, tolerance=0.8)
fig, ax2 = plt.subplots(ncols=1, figsize=(7, 5))
ax2.plot(contour[:, 0], contour[:, 1])

#these are all what i have tried
#plt.xticks(rotation='vertical')
# for tick in ax2.get_xticklabels():
    # tick.set_rotation(45)
#plt.setp(ax2.xaxis.get_majorticklabels(), rotation=70 )
#ax2.tick_params(axis='both', rotation=45)
#fig.autofmt_xdate(bottom=0.5, rotation=90, ha='right')
#plt.hist(ax2, bins=10, orientation='horizontal')


plt.axis('off')
plt.tick_params(axis='both' , left='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off', labelright='off', labelbottom='off')
plt.savefig("test.svg", format="svg")

The output is:

Expected output is:

Thanks in advance.

1条回答
欢心
2楼-- · 2019-08-09 15:27

There are a lot of options here. It is important to note that rotating the ticks will not help here. Instead, use either of the following.

  • Flip the axes using invert_yaxis(). This would not rotate the image, but flip the axes the image is shown in vertically.

    ax2.plot(contour[:, 1], contour[:, 0])
    ax2.invert_yaxis()
    
  • Flip the image using numpy.flipud. This would not rotate the image, but flip it vertically before processing it further.

    fimg = plt.imread(filename)
    fimg = np.flipud(fimg)
    # ...
    ax2.plot(contour[:, 1], contour[:, 0])
    
  • Rotate the image using numpy.rot90. In fact you would need to rotate it by 180 degrees (k=2).

    fimg = plt.imread(filename)
    fimg = np.rot90(fimg,k=2)
    # ...
    ax2.plot(contour[:, 1], contour[:, 0])
    
  • Rotate the output curve

    mat = lambda angle: np.array([[ np.cos(angle), np.sin(angle)],
                                  [-np.sin(angle), np.cos(angle)]])
    rotcontour = np.dot(contour, mat(np.deg2rad(180)))
    ax2.plot(rotcontour[:, 1], rotcontour[:, 0])
    
查看更多
登录 后发表回答