我想在索引上制作直方圖影片DataFrame,以便隨著索引的增加,垃圾箱會填滿。我DataFrame的結構是這樣的:
| 指數 | 成分 |
|---|---|
| 1 | 洋蔥 |
| 2 | 洋蔥 |
| 3 | 蒜 |
| 4 | 洋蔥 |
| 5 | 番茄 |
| 6 | 番茄 |
| 7 | 洋蔥 |
在影片開始時,所有的 bin 都應該初始化為 0,并隨著 index 的增加而被填滿。我嘗試了以下使用plotly.express.histogram.
idx = df.index
fig = px.histogram(df, x="Ingredient",
animation_frame=idx, animation_group=idx, cumulative=True,
)
我得到的結果是一個影片直方圖,其中只有一個 bin在通過 時在成分名稱之間切換,DataFrame并且 bin 的高度保持在 1 不變。
uj5u.com熱心網友回復:
- 方法是準備一個準備影片的資料框
- 然后很容易創建一個影片欄來創建您定義的圖形
import pandas as pd
import plotly.express as px
import io
df = pd.read_csv(
io.StringIO(
"""Index,Ingredient
1,Onions
2,Onions
3,Garlic
4,Onions
5,Tomato
6,Tomato
7,Onions"""
)
)
ings = df["Ingredient"].value_counts()
df2 = pd.concat(
[
pd.DataFrame({"Ingredient": ings.index})
.merge(
df.loc[df["Index"].le(i)].groupby("Ingredient", as_index=False).size(),
on="Ingredient",
how="left",
)
.assign(Index=i)
.fillna(0)
for i in range(df["Index"].min(), df["Index"].max() 1)
]
)
px.bar(df2, x="Ingredient", y="size", animation_frame="Index").update_layout(
yaxis={"range": [0, ings.max()]}
)

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415720.html
標籤:
上一篇:將具有唯一行ID列和串列列的PythonPandasDataFrames合并/合并到具有所有ID和統一、非重復串列的DataFrame?
