私はlena(有名な絵の名前)のヒストグラムを描くために必要な宿題があります。 そして、ここがmatplot.orgのコード例を、次に私のhistogram.Iで何かが間違っているとは私のコードです:Pythonの関数matplotlib.pyplot.histのいくつかの質問
import matplotlib.pyplot as plt
import numpy as np
def rgb2gray(rgb): # rgb to grey
temp = np.dot(rgb[..., :3], [0.299, 0.587, 0.114])
new = np.zeros([512,512], 'uint8')
for i in range(512):
for j in range(512):
new[i, j] = round(temp[i, j]) #float to int
return new
lena = plt.imread("E:\lena.bmp")
lena_gray = rgb2gray(lena)
len, width = lena_gray.shape
n, bins, patches = plt.hist(lena_gray, normed=1)
print(n ,bins, patches)
plt.show()
しかし、私が得るヒストグラムは次のとおりです。
間違い私は絵の下を拡大するときです:
ご覧のとおり、そこに私のコードでは、関数histの最初のパラメータはlena_grayです。配列lena_greyの数字はすべて整数です。だから私はヒストグラムで2つの数字の間に非常に多くのビンがあるのか、なぜx軸に小数点があるのかを知りたい。
あなたは代わりに、おそらく例のコードにリンク画像 –
へのリンクを投稿のあなたの出力を印刷する必要がありますか?これはmatplotlibの典型的な動作です。 – user2699