有一个 df:
import random
import pandas as pd
def get_row():
row = {"date" : random.choice(range(10)), "status" : random.choice(["won", "lost", "onoing"]), "id" : random.choice(range(10))}
return row
df = pd.DataFrame([get_row() for i in range(100)])
tab = df.pivot_table(index="date", columns="status", values="id", aggfunc="count")
tab
我可以尝试绘制:
tab.plot.bar(stacked=True)
如何固定每列的颜色? 我希望“失败”为红色,“获胜”为绿色,“正在进行”为蓝色。
好吧,我找到了答案:
gcolors = {'lost': 'red', 'onoing': 'blue', 'won': 'green'}
tab.plot.bar(stacked=True, color=[gcolors[group] for group in tab])
最佳答案
嗯,正如我所说,在提出问题时,我找到了答案:
gcolors = {'lost': 'red', 'onoing': 'blue', 'won': 'green'}
tab.plot.bar(stacked=True, color=[gcolors[group] for group in tab])
关于python - Pandas 情节 : fixing a color by value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51333793/