我使用 seaborn
创建了一个带有阴影的条形图。我还能够添加一个包含阴影样式的图例,如下所示:
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
hatches = ['\\\\', '//']
fig, ax = plt.subplots(figsize=(6,3))
sns.barplot(data=tips, x="day", y="total_bill", hue="time")
# loop through days
for hues, hatch in zip(ax.containers, hatches):
# set a different hatch for each time
for hue in hues:
hue.set_hatch(hatch)
# add legend with hatches
plt.legend().loc='best'
plt.show()
然而,当我尝试在具有图例的seaborn
上创建直方图时,相同的代码不起作用;我收到以下错误:No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
我已经在网上寻找答案,但没有成功找到。如何将阴影添加到下面的MWE直方图的图例中?
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
hatches = ['\\\\', '//']
fig, ax = plt.subplots(figsize=(6,3))
sns.histplot(data=tips, x="total_bill", hue="time", multiple='stack')
# loop through days
for hues, hatch in zip(ax.containers, hatches):
# set a different hatch for each time
for hue in hues:
hue.set_hatch(hatch)
# add legend with hatches
plt.legend().loc='best' # this does not work
plt.show()