首页 > 解决方案 > 如何在同一图中绘制水平图和垂直图?

问题描述

我想使用 matplotlib 在实际图像顶部用图形可视化二值化图像文件中黑色像素的数量。这是图像: 样本.png

我设法计算了垂直(x_counts)和水平(y_counts)的像素。我仍然想不通的是如何转置 的值y_counts,以便 y_counts-graph 与x_counts.

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img_file = "sample.png"
img = mpimg.imread(img_file)

#Count black pixels on x- and y-axis
x_counts = np.sum(img==0, axis=0)
y_counts = np.sum(np.transpose(img)==0, axis=0)

print(y_counts)
input()

#Show image
plt.imshow(img)

#Show number of pixels
plt.plot(x_counts)
plt.plot(y_counts) ### Problem: Graph as to be transposed
plt.show()

我得到的是:

图_1.png

但我想将橙色图形逆时针旋转 90°。

任何帮助将不胜感激!

标签: python-3.xmatplotlib

解决方案


推荐阅读