我的資料框如下所示:
| 客戶ID | 年齡 | 是真的 |
|---|---|---|
| 123 | 31 | 1 |
| 124 | 33 | 1 |
| 125 | 45 | 0 |
| 126 | 27 | 0 |
| 127 | 37 | 1 |
| 128 | 39 | 0 |
| 129 | 49 | 0 |
| 130 | 30 | 0 |
| 131 | 30 | 0 |
| 132 | 38 | 1 |
我可以像這樣創建年齡直方圖:
df.Age.hist()
plt.title('Age')
plt.xlabel('Age')
plt.ylabel('Frequency')
我會得到:

我想添加一個“是真的”欄位的圖例。對于每個 Bin,我想看看哪個部分是 1。我該怎么做?
uj5u.com熱心網友回復:
我不確定你可以用 Matplotlib 做到這一點。但我知道你可以使用 Plotly。
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill", color="sex")
fig.show()

更多資訊: https ://plotly.com/python/histograms/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/529731.html
