matplotlib 绘图旋转 90 度没有发生

lal*_*mar 1 svg matplotlib python-3.x

我正在使用 matplotlib 找到图像的边缘。我几乎已经完成了。我想在绘图中将图像旋转 90 度。但它对我不起作用。我尝试了很多东西。下面是我的代码试过了。

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")
Run Code Online (Sandbox Code Playgroud)

输出是:

预期输出为:

提前致谢。

Imp*_*est 6

这里有很多选择。重要的是要注意旋转刻度在这里无济于事。相反,请使用以下任一方法。

  • 翻转轴使用invert_yaxis()。这不会旋转图像,而是翻转图像垂直显示的轴。

    ax2.plot(contour[:, 1], contour[:, 0])
    ax2.invert_yaxis()
    
    Run Code Online (Sandbox Code Playgroud)
  • 翻转图像使用numpy.flipud。这不会旋转图像,而是在进一步处理之前垂直翻转它。

    fimg = plt.imread(filename)
    fimg = np.flipud(fimg)
    # ...
    ax2.plot(contour[:, 1], contour[:, 0])
    
    Run Code Online (Sandbox Code Playgroud)
  • 旋转图像使用numpy.rot90。事实上,您需要将其旋转 180 度 ( k=2)。

    fimg = plt.imread(filename)
    fimg = np.rot90(fimg,k=2)
    # ...
    ax2.plot(contour[:, 1], contour[:, 0])
    
    Run Code Online (Sandbox Code Playgroud)
  • 旋转输出曲线

    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])
    
    Run Code Online (Sandbox Code Playgroud)