如何为 seaborn barplot 中最大的条设置不同的颜色
How to set a different color to the largest bar in a seaborn barplot
我正在尝试创建一个条形图,其中所有小于最大条形的条形都是淡淡的颜色,而最大的条形是更鲜艳的颜色。 darkhorse analytic 的 pie chart gif 就是一个很好的例子,他们分解饼图并以更清晰的条形图结束。任何帮助将不胜感激,谢谢!
只需传递一个颜色列表。像
values = np.array([2,5,3,6,4,7,1])
idx = np.array(list('abcdefg'))
clrs = ['grey' if (x < max(values)) else 'red' for x in values ]
sb.barplot(x=idx, y=values, palette=clrs) # color=clrs)
(正如评论中指出的,Seaborn 的更高版本使用 "palette" 而不是 "color")
[条形图案例] 如果您从数据框中获取数据,您可以执行以下操作:
labels = np.array(df.Name)
values = np.array(df.Score)
clrs = ['grey' if (x < max(values)) else 'green' for x in values ]
#Configure the size
plt.figure(figsize=(10,5))
#barplot
sns.barplot(x=labels, y=values, palette=clrs) # color=clrs)
#Rotate x-labels
plt.xticks(rotation=40)
其他答案定义了 在 绘图之前的颜色。您也可以 afterwards 通过改变条形本身来完成它,它是您用于绘图的轴的补丁。要重新创建 iayork 的示例:
import seaborn
import numpy
values = numpy.array([2,5,3,6,4,7,1])
idx = numpy.array(list('abcdefg'))
ax = seaborn.barplot(x=idx, y=values) # or use ax=your_axis_object
for bar in ax.patches:
if bar.get_height() > 6:
bar.set_color('red')
else:
bar.set_color('grey')
您也可以直接通过例如ax.patches[7]
。使用 dir(ax.patches[7])
您可以显示您可以利用的 bar 对象的其他属性。
我是怎么做到的:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
bar = sns.histplot(data=data, x='Q1',color='#42b7bd')
# you can search color picker in google, and get hex values of you fav color
patch_h = [patch.get_height() for patch in bar.patches]
# patch_h contains the heights of all the patches now
idx_tallest = np.argmax(patch_h)
# np.argmax return the index of largest value of the list
bar.patches[idx_tallest].set_facecolor('#a834a8')
#this will do the trick.
我喜欢这种设置颜色优先或 post 通过读取最大值。我们不必担心补丁的数量或最高值是多少。
参考 matplotlib.patches.Patch
ps: 我对这里给出的情节进行了更多定制。上面给出的代码不会产生相同的结果。
我正在尝试创建一个条形图,其中所有小于最大条形的条形都是淡淡的颜色,而最大的条形是更鲜艳的颜色。 darkhorse analytic 的 pie chart gif 就是一个很好的例子,他们分解饼图并以更清晰的条形图结束。任何帮助将不胜感激,谢谢!
只需传递一个颜色列表。像
values = np.array([2,5,3,6,4,7,1])
idx = np.array(list('abcdefg'))
clrs = ['grey' if (x < max(values)) else 'red' for x in values ]
sb.barplot(x=idx, y=values, palette=clrs) # color=clrs)
(正如评论中指出的,Seaborn 的更高版本使用 "palette" 而不是 "color")
[条形图案例] 如果您从数据框中获取数据,您可以执行以下操作:
labels = np.array(df.Name)
values = np.array(df.Score)
clrs = ['grey' if (x < max(values)) else 'green' for x in values ]
#Configure the size
plt.figure(figsize=(10,5))
#barplot
sns.barplot(x=labels, y=values, palette=clrs) # color=clrs)
#Rotate x-labels
plt.xticks(rotation=40)
其他答案定义了 在 绘图之前的颜色。您也可以 afterwards 通过改变条形本身来完成它,它是您用于绘图的轴的补丁。要重新创建 iayork 的示例:
import seaborn
import numpy
values = numpy.array([2,5,3,6,4,7,1])
idx = numpy.array(list('abcdefg'))
ax = seaborn.barplot(x=idx, y=values) # or use ax=your_axis_object
for bar in ax.patches:
if bar.get_height() > 6:
bar.set_color('red')
else:
bar.set_color('grey')
您也可以直接通过例如ax.patches[7]
。使用 dir(ax.patches[7])
您可以显示您可以利用的 bar 对象的其他属性。
我是怎么做到的:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
bar = sns.histplot(data=data, x='Q1',color='#42b7bd')
# you can search color picker in google, and get hex values of you fav color
patch_h = [patch.get_height() for patch in bar.patches]
# patch_h contains the heights of all the patches now
idx_tallest = np.argmax(patch_h)
# np.argmax return the index of largest value of the list
bar.patches[idx_tallest].set_facecolor('#a834a8')
#this will do the trick.
我喜欢这种设置颜色优先或 post 通过读取最大值。我们不必担心补丁的数量或最高值是多少。
参考 matplotlib.patches.Patch