用箭头标记matplotlib直方图bin

时间:2016-03-03 18:24:56

标签: python matplotlib annotate

我有一个直方图,可以用下面的MWE复制:

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

pd.Series(np.random.normal(0, 100, 1000)).plot(kind='hist', bins=50)

这会创建一个这样的情节:

example histogram

然后我如何用给定整数的箭头标记bin?

例如,见下文,箭头标记包含整数300的bin。

example with bin of integer labelled

编辑:我应该理想地添加箭头的y坐标应该由它标记的条的高度自动设置 - 如果可能的话!

2 个答案:

答案 0 :(得分:6)

您可以使用annotate添加箭头:

import pandas as pd
import matplotlib.pyplot as plt
#import seaborn as sns
import numpy as np

fig, ax = plt.subplots()
series = pd.Series(np.random.normal(0, 100, 1000))
series.plot(kind='hist', bins=50, ax=ax)
ax.annotate("",
            xy=(300, 5), xycoords='data',
            xytext=(300, 20), textcoords='data',
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3"),
            )

在这个例子中,我添加了一个从坐标(300,20)到(300,5)的箭头。

为了自动将箭头缩放到bin中的值,您可以使用matplotlib hist绘制直方图并获取值,然后使用numpy where查找哪个bin对应于理想的位置。

import pandas as pd
import matplotlib.pyplot as plt
#import seaborn as sns
import numpy as np

nbins = 50
labeled_bin = 200

fig, ax = plt.subplots()

series = pd.Series(np.random.normal(0, 100, 1000))

## plot the histogram and return the bin position and values
ybins, xbins, _ = ax.hist(series, bins=nbins)

## find out in which bin belongs the position where you want the label
ind_bin = np.where(xbins >= labeled_bin)[0]
if len(ind_bin) > 0 and ind_bin[0] > 0:
    ## get position and value of the bin
    x_bin = xbins[ind_bin[0]-1]/2. + xbins[ind_bin[0]]/2.
    y_bin = ybins[ind_bin[0]-1]
    ## add the arrow
    ax.annotate("",
                xy=(x_bin, y_bin + 5), xycoords='data',
                xytext=(x_bin, y_bin + 20), textcoords='data',
                arrowprops=dict(arrowstyle="->",
                                connectionstyle="arc3"),
                                )
else:
    print "Labeled bin is outside range"

答案 1 :(得分:2)

我认为@Julien Spronck展示了最佳方式。或者,您也可以使用arrow;示例代码可以在下面找到。 y-ccordinate是通过计算某个bin中有多少元素(具有您自己定义的特定容差)自动确定的。您可以使用参数(箭头长度,箭头长度)。这是代码:

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

mySer = pd.Series(np.random.normal(0, 100, 1000))
mySer.plot(kind='hist', bins=50)

# that is where you want to add the arrow
ind = 200
# determine how many elements you have in the bin (with a certain tolerance)
n = len(mySer[(mySer > ind*0.95) & (mySer <  ind*1.05)])

# define length of the arrow
lenArrow = 10
lenHead = 2
wiArrow = 5
plt.arrow(ind, n+lenArrow+lenHead, 0, -lenArrow, head_width=wiArrow+3, head_length=lenHead, width=wiArrow, fc='k', ec='k')

plt.show()

这为您提供了以下输出(如示例中的200而不是300):

enter image description here