在 matplotlib 中使用 barplot 更改某些条形标签的颜色
Change color at certain bar labels with barplot in matplotlib
我尝试为标签绘制具有特定颜色的条形图,并为每种类型制作带有颜色的图例。例如,标签“类型 1”、“类型 2”和“类型 3”每种颜色对应于每个标签。谢谢!
import matplotlib.pyplot as plt
import pandas as pd
mdict={"Types":["A","B","C","D","F", "G"],"Count":[3,4,5,6,7,6]}
df=pd.DataFrame(mdict)
fig, ax=plt.subplots(figsize=(15,8))
for i in df.Types:
if i in ["A","B"]:
ax.bar(df.Types,df.Count,color="red", label="Type 1")
elif i in ["C","D"]:
ax.bar(df.Types,df.Count,color="green", label="Type 2")
else:
ax.bar(df.Types,df.Count, color="blue", label="Type 3")
ax.legend()
您可以为标签添加一个额外的列,并使用 seaborn 创建绘图:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
mdict = {"Types": ["A", "B", "C", "D", "F", "G"], "Count": [3, 4, 5, 6, 7, 6]}
df = pd.DataFrame(mdict)
df["Label"] = "Type 3"
df.loc[df["Types"].isin(["A", "B"]), "Label"] = "Type 1"
df.loc[df["Types"].isin(["C", "D"]), "Label"] = "Type 2"
fig, ax = plt.subplots(figsize=(15, 8))
palette = {"Type 1": "crimson", "Type 2": "limegreen", "Type 3": "dodgerblue"}
sns.barplot(data=df, x="Types", y="Count", hue="Label", palette=palette, dodge=False, ax=ax)
plt.show()
没有 seaborn 的另一种方法是创建两列:一列用于标签,一列用于数字索引。最近的 matplotlib 版本接受 data=
关键字,以指示数据帧(或数据帧的子集)。需要数字索引才能在正确位置绘制条形。
import matplotlib.pyplot as plt
import pandas as pd
mdict = {"Types": ["A", "B", "C", "D", "F", "G"], "Count": [3, 4, 5, 6, 7, 6]}
df = pd.DataFrame(mdict)
df["Label"] = ["Type 1" if i in ["A", "B"] else "Type 2" if i in ["C", "D"] else "Type 3" for i in df["Types"]]
df["ind"] = range(len(df))
fig, ax = plt.subplots(figsize=(15, 8))
ax.bar("ind", "Count", color="crimson", label="Type 1",
data=df.loc[df["Types"].isin(["A", "B"])])
ax.bar("ind", "Count", color="limegreen", label="Type 2",
data=df.loc[df["Types"].isin(["C", "D"])])
ax.bar("ind", "Count", color="dodgerblue", label="Type 2",
data=df.loc[~ (df["Types"].isin(["A", "B"]) | df["Types"].isin(["C", "D"]))])
ax.set_xticks(df["ind"], df["Types"])
ax.legend()
plt.show()
我尝试为标签绘制具有特定颜色的条形图,并为每种类型制作带有颜色的图例。例如,标签“类型 1”、“类型 2”和“类型 3”每种颜色对应于每个标签。谢谢!
import matplotlib.pyplot as plt
import pandas as pd
mdict={"Types":["A","B","C","D","F", "G"],"Count":[3,4,5,6,7,6]}
df=pd.DataFrame(mdict)
fig, ax=plt.subplots(figsize=(15,8))
for i in df.Types:
if i in ["A","B"]:
ax.bar(df.Types,df.Count,color="red", label="Type 1")
elif i in ["C","D"]:
ax.bar(df.Types,df.Count,color="green", label="Type 2")
else:
ax.bar(df.Types,df.Count, color="blue", label="Type 3")
ax.legend()
您可以为标签添加一个额外的列,并使用 seaborn 创建绘图:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
mdict = {"Types": ["A", "B", "C", "D", "F", "G"], "Count": [3, 4, 5, 6, 7, 6]}
df = pd.DataFrame(mdict)
df["Label"] = "Type 3"
df.loc[df["Types"].isin(["A", "B"]), "Label"] = "Type 1"
df.loc[df["Types"].isin(["C", "D"]), "Label"] = "Type 2"
fig, ax = plt.subplots(figsize=(15, 8))
palette = {"Type 1": "crimson", "Type 2": "limegreen", "Type 3": "dodgerblue"}
sns.barplot(data=df, x="Types", y="Count", hue="Label", palette=palette, dodge=False, ax=ax)
plt.show()
没有 seaborn 的另一种方法是创建两列:一列用于标签,一列用于数字索引。最近的 matplotlib 版本接受 data=
关键字,以指示数据帧(或数据帧的子集)。需要数字索引才能在正确位置绘制条形。
import matplotlib.pyplot as plt
import pandas as pd
mdict = {"Types": ["A", "B", "C", "D", "F", "G"], "Count": [3, 4, 5, 6, 7, 6]}
df = pd.DataFrame(mdict)
df["Label"] = ["Type 1" if i in ["A", "B"] else "Type 2" if i in ["C", "D"] else "Type 3" for i in df["Types"]]
df["ind"] = range(len(df))
fig, ax = plt.subplots(figsize=(15, 8))
ax.bar("ind", "Count", color="crimson", label="Type 1",
data=df.loc[df["Types"].isin(["A", "B"])])
ax.bar("ind", "Count", color="limegreen", label="Type 2",
data=df.loc[df["Types"].isin(["C", "D"])])
ax.bar("ind", "Count", color="dodgerblue", label="Type 2",
data=df.loc[~ (df["Types"].isin(["A", "B"]) | df["Types"].isin(["C", "D"]))])
ax.set_xticks(df["ind"], df["Types"])
ax.legend()
plt.show()