设置yticks颜色以匹配条形图颜色python

2024-06-15 05:58:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何设置y名称来匹配对应的条形图的颜色。所以每个发行商的名字都有和它旁边的栏一样的颜色,比如PS2会是粉色,3DS是绿色等等。在

f, ax=plt.subplots(figsize=(7,7))
sns.barplot(x=gbyplat,y=gbyplat.index, palette='husl')
plt.ylabel('Publisher', color='deepskyblue', size=15, alpha=0.8)
plt.xlabel('Number of titles published', color='slateblue', size=15, alpha=0.7)

image


Tags: alpha名称size颜色pltax名字color
1条回答
网友
1楼 · 发布于 2024-06-15 05:58:55

您可以迭代ticklabels并从调色板设置相应的颜色。在

enter image description here

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

l =list("ABCDEFGHIJK")
x = np.arange(1,len(l)+1)[::-1]
f, ax=plt.subplots(figsize=(7,7))
sns.barplot(x=x,y=l, palette='husl', ax=ax)
plt.ylabel('Publisher', color='deepskyblue', size=15, alpha=0.8)
plt.xlabel('Number of titles published', color='slateblue', size=15, alpha=0.7)

p = sns.color_palette("husl", len(l))
for i, label in enumerate(ax.get_yticklabels()):
    label.set_color(p[i])

plt.show()

相关问题 更多 >