最佳答案
定义离散颜色图。在下面的代码中: c = dict(zip(df["emotion"].unique(), px.colors.qualitative.G10))
import pandas as pd
import numpy as np
import plotly.express as px
# create some data
df = pd.DataFrame({"date":np.random.choice(pd.date_range("1-sep-2020","31-Dec-2020"),300),
"emotion":np.random.choice(["positive","negative","anticipation","fear","trust"], 300)}).sort_values("date")
# map emotions to a color
c = dict(zip(df["emotion"].unique(), px.colors.qualitative.G10))
# bar chart
px.bar(
df.groupby(["date", "emotion"], as_index=False)
.size()
.rename(columns={"size": "count"}),
x="date",
y="count",
color="emotion",
color_discrete_map=c
).show()
# pie chart
px.pie(
df.groupby("emotion", as_index=False).agg(
perc=("date", lambda s: len(s) / len(df))
),
values="perc",
names="emotion",
color="emotion",
color_discrete_map=c
).show()
关于python - Plotly:如何对两个不同图的类别使用相同的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68208827/