日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中matplotlib关于直方图AttributeError: ‘Rectangle‘ object has no property ‘normed‘的解决方法

發布時間:2025/4/5 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中matplotlib关于直方图AttributeError: ‘Rectangle‘ object has no property ‘normed‘的解决方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • 遇到的問題
      • 解決方法
      • 參考

3秒版本:

改成如下形式即可,去掉normed,改成density(布爾值),意思是開啟概率分布(直方圖面積為1).

plt.hist(hist_r, bins = 40, density=True,facecolor="red", edgecolor="black", alpha=0.7)

遇到的問題

今天在閱讀《數字圖像處理與python實現》一書中的圖像直方圖實驗時,想繪制彩色圖片三通道直方圖,但是書中并沒有給出源碼。
只是給出了計算直方圖的部分代碼

# 計算直方圖 hist_r = exposure.histogram(image[:, :, 0], nbins = 256) hist_g = exposure.histogram(image[:, :, 1], nbins = 256) hist_b = exposure.histogram(image[:, :, 2], nbins = 256)

然后在網上去搜尋三通道直方圖的畫法,還真找到了:python數字圖像處理(9):直方圖與均衡化
其中給出的方法是調用hist方法來繪制:

n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black',edgecolor='black',alpha=1,histtype='bar')

然后就按照這種方式來做,結果就是報錯:

AttributeError: 'Rectangle' object has no property 'normed'

嘗試刪除normed,可以運行,結果如下:得到并不是歸一化的直方圖,和書中給的可視化圖形也并不一致。

用于測試的代碼

from skimage import exposure, io, data from matplotlib import pyplot as plt import matplotlib import numpy as np# 設置matplotlib正常顯示中文和負號 matplotlib.rcParams['font.sans-serif']=['SimHei'] # 用黑體顯示中文 matplotlib.rcParams['axes.unicode_minus']=False # 正常顯示負號image = data.coffee()# 計算直方圖 hist_r = exposure.histogram(image[:, :, 0], nbins = 256) hist_g = exposure.histogram(image[:, :, 1], nbins = 256) hist_b = exposure.histogram(image[:, :, 2], nbins = 256)plt.subplot(2, 2, 1) plt.title("原圖") io.imshow(image)# data = np.random.randn(10000) plt.subplot(2, 2, 2) plt.title("R通道顏色直方圖") plt.hist(hist_r, bins = 40, facecolor="red", edgecolor="black", alpha=0.7)plt.subplot(2, 2, 3) plt.title("G通道顏色直方圖") plt.hist(hist_g, bins = 40, facecolor="green", edgecolor="black", alpha=0.7)plt.subplot(2, 2, 4) plt.title("B通道顏色直方圖") plt.hist(hist_b, bins = 40, facecolor="blue", edgecolor="black", alpha=0.7)plt.show()

解決方法

然后在stackoverflow和其他博文中翻閱了一下,最終還是打開了matplotlib 的開發文檔
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html#matplotlib.pyplot.hist
這個hist是在matplotlib.pyplot下的:
這是hist函數原型

matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype=‘bar’, align=‘mid’, orientation=‘vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, *, data=None, **kwargs)

其中關于density參數的解釋

densitybool, default: False
If True, draw and return a probability density: each bin will display the bin’s raw count divided by the total number of counts and the bin width (density = counts / (sum(counts) * np.diff(bins))), so that the area under the histogram integrates to 1 (np.sum(density * np.diff(bins)) == 1).
If stacked is also True, the sum of the histograms is normalized to 1.

density是概率密度的開關,如果density=True,就是直方圖歸一化,面積加起來為1.

所以改成:

plt.hist(hist_r, bins = 40, density=True,facecolor="red", edgecolor="black", alpha=0.7)


代碼

from skimage import data, io import matplotlib import matplotlib.pyplot as plt# 設置matplotlib正常顯示中文和負號 matplotlib.rcParams['font.sans-serif']=['SimHei'] # 用黑體顯示中文 matplotlib.rcParams['axes.unicode_minus']=False # 正常顯示負號image=data.coffee() # flatten 將矩陣降維一維 hist_r=image[:,:,0].flatten() hist_g=image[:,:,1].flatten() hist_b=image[:,:,2].flatten()plt.subplot(2, 2, 1) plt.title("原圖") io.imshow(image)# data = np.random.randn(10000) plt.subplot(2, 2, 2) plt.title("R通道顏色直方圖") plt.hist(hist_r, bins = 40, density=True,facecolor="red", edgecolor="black", alpha=0.7)plt.subplot(2, 2, 3) plt.title("G通道顏色直方圖") plt.hist(hist_g, bins = 40, density=True,facecolor="green", edgecolor="black", alpha=0.7)plt.subplot(2, 2, 4) plt.title("B通道顏色直方圖") plt.hist(hist_b, bins = 40, density=True,facecolor="blue", edgecolor="black", alpha=0.7)plt.show()

參考

[1]https://blog.csdn.net/qq_45069279/article/details/105636669
[2]https://blog.csdn.net/haoyu_xie/article/details/106814610
[3]https://matplotlib.org/stable/api/index.htmlmatplotlib參考API

總結

以上是生活随笔為你收集整理的python中matplotlib关于直方图AttributeError: ‘Rectangle‘ object has no property ‘normed‘的解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。