我问这个问题是因为我还没有找到一个有效的示例来说明如何对分组的水平Pandas条形图进行注释。我知道以下两个:
但是它们都是关于垂直条形图的。即,要么没有水平条形图的解决方案,要么它不能完全正常工作。
经过几个星期的研究,我终于可以用示例代码提出问题,这几乎是我想要的,只是不能100%地工作。需要您的帮助才能达到那100%。
到这里,完整的代码在这里上传。结果看起来像这样:
您会看到它几乎可以正常工作,只是标签没有放在我想要的位置,我也无法自己将它们移到更好的位置。此外,由于图表栏的顶部用于显示错误栏,所以我真正想要的是将带注释的文本移向y轴,在y轴的左侧或右侧很好地对齐,具体取决于X -值。例如,这是我的同事可以使用MS Excel进行的操作:
Python是否可以通过Pandas图表做到这一点?
我在上面的网址中包含了用于注释的代码,一个是我所能做的,另一个是供参考(来自In [23]
):
# my all-that-I-can-do
def autolabel(rects):
#if height constant: hbars, vbars otherwise
if (np.diff([plt.getp(item, 'width') for item in rects])==0).all():
x_pos = [rect.get_x() + rect.get_width()/2. for rect in rects]
y_pos = [rect.get_y() + 1.05*rect.get_height() for rect in rects]
scores = [plt.getp(item, 'height') for item in rects]
else:
x_pos = [rect.get_width()+.3 for rect in rects]
y_pos = [rect.get_y()+.3*rect.get_height() for rect in rects]
scores = [plt.getp(item, 'width') for item in rects]
# attach some text labels
for rect, x, y, s in zip(rects, x_pos, y_pos, scores):
ax.text(x,
y,
#'%s'%s,
str(round(s, 2)*100)+'%',
ha='center', va='bottom')
# for the reference
ax.bar(1. + np.arange(len(xv)), xv, align='center')
# Annotate with text
ax.set_xticks(1. + np.arange(len(xv)))
for i, val in enumerate(xv):
ax.text(i+1, val/2, str(round(val, 2)*100)+'%', va='center',
ha='center', color='black')
请帮忙。谢谢。
因此,为简单起见,我对构建数据的方式进行了一些更改:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
sns.set_style("white") #for aesthetic purpose only
# fake data
df = pd.DataFrame({'A': np.random.choice(['foo', 'bar'], 100),
'B': np.random.choice(['one', 'two', 'three'], 100),
'C': np.random.choice(['I1', 'I2', 'I3', 'I4'], 100),
'D': np.random.randint(-10,11,100),
'E': np.random.randn(100)})
p = pd.pivot_table(df, index=['A','B'], columns='C', values='D')
e = pd.pivot_table(df, index=['A','B'], columns='C', values='E')
ax = p.plot(kind='barh', xerr=e, width=0.85)
for r in ax.patches:
if r.get_x() < 0: # it it's a negative bar
ax.text(0.25, # set label on the opposite side
r.get_y() + r.get_height()/5., # y
"{:" ">7.1f}%".format(r.get_x()*100), # text
bbox={"facecolor":"red",
"alpha":0.5,
"pad":1},
fontsize=10, family="monospace", zorder=10)
else:
ax.text(-1.5, # set label on the opposite side
r.get_y() + r.get_height()/5., # y
"{:" ">6.1f}%".format(r.get_width()*100),
bbox={"facecolor":"green",
"alpha":0.5,
"pad":1},
fontsize=10, family="monospace", zorder=10)
plt.tight_layout()
这使:
我根据平均值绘制标签,并将其放在0线的另一侧,因此您可以确定它永远不会与其他东西重叠,除了有时会出现误差条。我在文本后面设置了一个方框,以反映均值的值。您需要根据图形尺寸调整一些值,以便标签正确放置,例如:
width=0.85
+r.get_height()/5. # y
"pad":1
fontsize=10
"{:" ">6.1f}%".format(r.get_width()*100)
:在标签中设置字符总数(此处最小为6,如果少于6个字符,则在右侧填充空白)。它需要family="monospace"
告诉我是否不清楚。
高温超导
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句