让 seaborn 条形图中的颜色与轴中的标签相匹配
Have colors in a seaborn bar plot match the labels in the axis
我在 python 中使用 seaborn 生成了一个条形图,如下所示:
x 轴将始终包含颜色。有没有一种简单的方法可以让条形的颜色与 x 轴上的颜色相匹配?
到目前为止我的代码:
plt.figure(figsize=(12,8))
slippers_plot = sns.barplot(data=slipper_colour,x='colour', y='total_units', palette='bright')
slippers_plot
slippers_plot.set_title("Top Slipper Colors", fontsize=20, pad=30, fontdict={"weight": "bold"})
slippers_plot.set_xlabel("Total Units", fontsize=16)
slippers_plot.set_ylabel("", fontsize=18)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
如果您对 how web colors are named 满意,您可以只使用调色板的 x 值:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
slipper_colour = pd.DataFrame({'colour': ['Green', 'Black', 'Brown', 'Gray'],
'total_units': np.random.randint(10, 50, 4)})
plt.figure(figsize=(12, 8))
ax = sns.barplot(data=slipper_colour, x='colour', y='total_units', palette=slipper_colour['colour'])
ax.set_title("Top Slipper Colors", fontsize=20, pad=30, fontdict={"weight": "bold"})
ax.set_xlabel("Total Units", fontsize=16)
ax.set_ylabel("", fontsize=18)
ax.tick_params(labelsize=14)
plt.show()
要微调颜色,可以使用字典,例如
palette = {'Green': 'forestgreen', 'Black': 'black', 'Brown': 'sienna', 'Gray': 'silver'}
十六进制颜色(例如 '#7B68EE'
)也可以代替名称。
我在 python 中使用 seaborn 生成了一个条形图,如下所示:
x 轴将始终包含颜色。有没有一种简单的方法可以让条形的颜色与 x 轴上的颜色相匹配?
到目前为止我的代码:
plt.figure(figsize=(12,8))
slippers_plot = sns.barplot(data=slipper_colour,x='colour', y='total_units', palette='bright')
slippers_plot
slippers_plot.set_title("Top Slipper Colors", fontsize=20, pad=30, fontdict={"weight": "bold"})
slippers_plot.set_xlabel("Total Units", fontsize=16)
slippers_plot.set_ylabel("", fontsize=18)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
如果您对 how web colors are named 满意,您可以只使用调色板的 x 值:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
slipper_colour = pd.DataFrame({'colour': ['Green', 'Black', 'Brown', 'Gray'],
'total_units': np.random.randint(10, 50, 4)})
plt.figure(figsize=(12, 8))
ax = sns.barplot(data=slipper_colour, x='colour', y='total_units', palette=slipper_colour['colour'])
ax.set_title("Top Slipper Colors", fontsize=20, pad=30, fontdict={"weight": "bold"})
ax.set_xlabel("Total Units", fontsize=16)
ax.set_ylabel("", fontsize=18)
ax.tick_params(labelsize=14)
plt.show()
要微调颜色,可以使用字典,例如
palette = {'Green': 'forestgreen', 'Black': 'black', 'Brown': 'sienna', 'Gray': 'silver'}
十六进制颜色(例如 '#7B68EE'
)也可以代替名称。