matplotlib / matplotlib/matplotlib
[Bug]: Misalignment of Labels and Incorrect Density Values in `ax.hist()` for Categorical Variables
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 23.2k
- 分支
- 8.5k
- 平均合併
- 1 天 6 小時
- 30 天內合併 PR
- 66
描述
Bug summary
I have stumbled upon an issue while using ax.hist() with categorical variables and setting density = True. The issue arises from the following behaviors:
- Incorrect density computation: When a categorical variable is being used, the function internally converts the variable to integers (seen here). Consequently, the bin width isn't necessarily 1, leading to miscalculated density values. Underlying function
np.histogram, which is used internally, takes into account the bar width while ensuring the entire area equates to 1. However, when dealing with categoricals, we'd expect the sum of the column heights to be 1. - Misalignment of labels: The function doesn't position labels precisely in the center of the bars. To align the x-ticks at the centers, one could ostensibly utilize the
binsthat are produced by the function. However, it's challenging to unveil how the 'category to int' conversion (mentioned above) was performed. From the code here, it seems the elements in the first array are converted (in case there are multiple arrays) in the order they emerge, with the resultant map used to convert elements of the second array. If new categories are introduced, they are assigned the subsequent free integer. Unfortunately, there are no documented details about this process.
Code for reproduction
fig, ax = plt.subplots()
ax.hist(["a","b","c","d"], bins=4, density=True)
Actual outcome
(array([0.33333333, 0.33333333, 0.33333333, 0.33333333]), array([0. , 0.75, 1.5 , 2.25, 3. ]), <BarContainer object of 4 artists>)
Expected outcome
In the case the density parameter is passed I would expect that the sum of the columns would be 1 as we are dealing with categorical variables and the width of the bar should not be taken into account. Also it would be nice to be able to know how they are converted to integer to be able to plot the labels correctly in the plot.
Additional information
Proposed improvements to the above behaviors:
- It may be beneficial to reconsider how the function calculates densities when managing categorical data, explicitly setting the bin width to 1.
- The function could be enhanced to bijectively handle the 'category to int' conversion and provide clear documentation to make it more trustworthy.
- A solution needs to be implemented to ensure labels are correctly positioned at the center of the bars.
Operating system
OS/X
Matplotlib Version
3.8.3
Matplotlib Backend
MacOSX
Python version
3.10.14
Jupyter version
No response
Installation
conda
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
Start in lib/matplotlib/axes/_axes.py around the categorical conversion referenced in the issue, then run the provided ax.hist() reproduction. Compare the density values and bar-label positions with the expected categorical behavior; done means the density and category-to-position behavior are corrected and documented.
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- numpy, python
- 領域
- data-visualization
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100