python - 如何在 Seaborn barplot Python 中将数据标签注释添加到基于名称的单个条
问题描述
我有以下数据框产生以下情节:
# Import pandas library
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# initialize list of lists
data = [['tom', 10,1,'a'], ['matt', 15,5,'b'],['nick', 18,2,'b'],['luke', 12,6,'b'],['geoff', 20,10,'a']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Attempts','Score','Category'])
df
# Create plot
f, ax = plt.subplots(figsize=(8.27,11.7,))
df = df.sort_values(['Attempts'],ascending=False)
sns.set_color_codes("muted")
sns.barplot(x="Attempts", y="Name", data=df,
label="Total", palette=["b" if x!='nick' else 'r' for x in df.Name], ax=ax)
# Annotate every single bar with its value
for p in ax.patches:
width = p.get_width()
ax.text(width - 1,
p.get_y() + p.get_height() / 1 + 0.1,
'{:1.2f}'.format(width),ha="center")
我只想包含数据标签注释'Nick'
(即在这种情况下,条本身的值 18)是突出显示的条,并且在其余条上没有标签注释。是否有捷径可寻?非常感谢 !
解决方案
欢迎来到 StackOverflow。这是我的解决方案:
首先我重置您的 df 的索引,然后找到符合条件的行的索引号(在这种情况下nick
):
# Create plot
f, ax = plt.subplots(figsize=(8.27,11.7,))
df = df.sort_values(['Attempts'],ascending=False)
#Reset index values
df = df.reset_index(drop=True)
#Return the index number of name required
indexno = df[df['Name'] == 'nick'].index
#Create array
a = np.array(indexno)
a= a[0]
print(a)
这给了我:
1
(您的第二行df
)
然后我找到了所有的get_width
值:
#Plot
sns.set_color_codes("muted")
sns.barplot(x="Attempts", y="Name", data=df,
label="Total", palette=["b" if x!='nick' else 'r' for x in df.Name], ax=ax)
# Final all width values in plot
values = []
for i in ax.patches:
values.append(i.get_width())
print(values)
返回:
[20.0, 18.0, 15.0, 12.0, 10.0]
现在最后一个循环仅显示条件匹配的值(因此值 (18) 中的第二个值)。
#Final loop to annotate only the row where width equals the value of `nick`
for p in ax.patches:
width = p.get_width()
x = values[a]
if width==x:
ax.text(width - 1,
p.get_y() + p.get_height() / 1 + 0.1,
'{:1.2f}'.format(width),ha="center")
plt.show()
完整代码:
# Import pandas library
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# initialize list of lists
data = [['tom', 10,1,'a'], ['matt', 15,5,'b'],['nick', 18,2,'b'],['luke', 12,6,'b'],['geoff', 20,10,'a']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Attempts','Score','Category'])
df
# Create plot
f, ax = plt.subplots(figsize=(8.27,11.7,))
df = df.sort_values(['Attempts'],ascending=False)
#Reset index values
df = df.reset_index(drop=True)
#Return the index number of name required
indexno = df[df['Name'] == 'nick'].index
#Create array
a = np.array(indexno)
a = a[0]
print((a))
#Plot
sns.set_color_codes("muted")
sns.barplot(x="Attempts", y="Name", data=df,
label="Total", palette=["b" if x!='nick' else 'r' for x in df.Name], ax=ax)
# Final all width values in plot
values = []
for i in ax.patches:
values.append(i.get_width())
print(values[a])
#Final loop to annotate only the
for p in ax.patches:
width = p.get_width()
x = values[a]
if width==x:
ax.text(width - 1,
p.get_y() + p.get_height() / 1 + 0.1,
'{:1.2f}'.format(width),ha="center")
plt.show()
推荐阅读
- html - 如何用替代图像替换 HTML 图像?
- assembly - 汇编 jmp 到不存在的行
- agda - 单价公理是内射的吗?
- javascript - Javascript getElementById 但保留旧值
- android - 服务 stopForeground(false) 不应该删除通知
- docker - Docker 撰写未安装在 Windows 容器上的卷
- jquery - 将路径字符串传递给 jquery 函数
- electron - 如何在 BrowserWindow 中加载特定的 html 文件
- openshift - Openshift Web 控制台 - 运行扩展脚本不起作用
- charles-proxy - 查尔斯没有显示表格“连接来自”