绘制有条件的 seaborn histplot bar_label
Plotting seaborn histplot bar_label with condition
我想绘制一个 seaborn histogram with labels 来显示每个柱的值。我只想显示非零值,但我不确定该怎么做。我的 MWE 是
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
xlist = 900+200*np.random.randn(50,1)
fig, ax = plt.subplots()
y = sns.histplot(data=xlist, element="bars", bins=20, stat='count', legend=False)
y.set(xlabel='total time (ms)')
y.bar_label(y.containers[0])
## y.bar_label(y.containers[0][y.containers[0]!=0])
plt.show()
图形看起来像
我想删除所有 0
标签。
更新
@BigBen 建议的最佳版本:
labels = [str(v) if v else '' for v in y.containers[0].datavalues]
y.bar_label(y.containers[0], labels=labels)
尝试:
labels = []
for p in y.patches:
h = p.get_height()
labels.append(str(h) if h else '')
y.bar_label(y.containers[0], labels=labels)
我想绘制一个 seaborn histogram with labels 来显示每个柱的值。我只想显示非零值,但我不确定该怎么做。我的 MWE 是
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
xlist = 900+200*np.random.randn(50,1)
fig, ax = plt.subplots()
y = sns.histplot(data=xlist, element="bars", bins=20, stat='count', legend=False)
y.set(xlabel='total time (ms)')
y.bar_label(y.containers[0])
## y.bar_label(y.containers[0][y.containers[0]!=0])
plt.show()
图形看起来像
我想删除所有 0
标签。
更新
@BigBen 建议的最佳版本:
labels = [str(v) if v else '' for v in y.containers[0].datavalues]
y.bar_label(y.containers[0], labels=labels)
尝试:
labels = []
for p in y.patches:
h = p.get_height()
labels.append(str(h) if h else '')
y.bar_label(y.containers[0], labels=labels)