Python 用箭头标记matplotlib直方图箱

Python 用箭头标记matplotlib直方图箱,python,matplotlib,annotate,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) 这将创建如下图: 然后,我将如何用一个箭头标记给定整数的箱子 例如,请参见下文,其中箭头标记包含整数300的箱子 编辑:我

我有一个柱状图,可以用下面的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)
这将创建如下图:

然后,我将如何用一个箭头标记给定整数的箱子

例如,请参见下文,其中箭头标记包含整数300的箱子


编辑:我应该添加理想的y坐标的箭头应该是自动设置的高度栏,它是标签-如果可能的话

您可以使用
注释
添加箭头:

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)的箭头

为了将箭头自动缩放到存储箱中的值,可以使用matplotlib
hist
绘制直方图并返回值,然后使用numpy
where
查找哪个存储箱对应于所需位置

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"

@我认为朱利安·斯普朗克展示了最好的方式。或者,您也可以使用;下面可以找到示例代码。y坐标是通过计算某个箱子中有多少个图元自动确定的(具有您可以自己定义的特定公差)。您可以使用这些参数(箭头的长度、箭头的长度)。代码如下:

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()
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
导入seaborn作为sns
将numpy作为np导入
mySer=pd.级数(np.随机.正态(010000))
mySer.plot(kind='hist',bin=50)
#这就是要添加箭头的位置
ind=200
#确定箱子中有多少图元(具有一定公差)
n=len(mySer[(mySer>ind*0.95)和(mySer
这将为您提供以下输出(对于200而不是示例中的300):


感谢您的回复,为了进一步扩展此问题,是否可以根据垃圾箱的高度自动设置箭头的y位置?@BML91是的,可能。。。让我编辑一下我的答案,效果非常好。非常感谢。我正在查看文档以更改箭头线的重量,我似乎找不到任何东西-您可以为我指出正确的方向吗?我看到了
arrowprops()的
width
关键字
但是,当我尝试使用它时,我得到了一个
属性错误:未知属性宽度
。@BML91:
宽度
在下面的解决方案中运行良好(我更新了代码)。对于这个答案,您必须使用
lw
而不是
width
isnside
arrowprops()
;那么它就可以正常工作了。