python图形统计代码_直接在图形上显示matplotlib柱状图bin计数

博主尝试在Python中使用pandas和matplotlib展示直方图,并希望在每个柱子上直接显示计数。他们遇到的问题是尝试在直方图上添加文本标签时出现了错误。错误源于`hist`函数的参数问题,导致`max`必须大于`min`的范围限制未被满足。
摘要由CSDN通过智能技术生成

请允许我想我有一个简单的问题,但我看不到任何有用的博客显示如何实现这一点。我有一个名为“系列”的python pandas系列,我使用系列历史()将直方图可视化。我需要直接在图表上显示每个箱子的出现次数,但我找不到解决方法。在

如何在每个箱子的顶部看到一个标签,显示每个箱子的出现次数?在

准确地说,这是我的代码:import matplotlib.pyplot as plt

your_bins=10

data = [df_5m_9_4pm.loc['2017-6']['sum_daily_cum_ret'].values]

plt.hist(data, binds = your_bins)

arr = plt.hist(data,bins = your_bins)

for i in range(your_bins):

plt.text(arr[1][i],arr[0][i],str(arr[0][i]))

如果我简单地打印变量“data”,它看起来是这样的:

^{pr2}$

如果我运行上面的代码,我会得到错误消息:---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

in ()

2 your_bins=10

3 data = [df_5m_9_4pm.loc['2017-6']['sum_daily_cum_ret'].values]

----> 4 plt.hist(data, binds = your_bins)

5 arr = plt.hist(data,bins = your_bins)

6 for i in range(your_bins):

~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in hist(x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, normed, hold, data, **kwargs)

3002 histtype=histtype, align=align, orientation=orientation,

3003 rwidth=rwidth, log=log, color=color, label=label,

-> 3004 stacked=stacked, normed=normed, data=data, **kwargs)

3005 finally:

3006 ax._hold = washold

~/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)

1708 warnings.warn(msg % (label_namer, func.__name__),

1709 RuntimeWarning, stacklevel=2)

-> 1710 return func(ax, *args, **kwargs)

1711 pre_doc = inner.__doc__

1712 if pre_doc is None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in hist(***failed resolving arguments***)

6205 # this will automatically overwrite bins,

6206 # so that each histogram uses the same bins

-> 6207 m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)

6208 m = m.astype(float) # causes problems later if it's an int

6209 if mlast is None:

~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in histogram(a, bins, range, normed, weights, density)

665 if mn > mx:

666 raise ValueError(

--> 667 'max must be larger than min in range parameter.')

668 if not np.all(np.isfinite([mn, mx])):

669 raise ValueError(

ValueError: max must be larger than min in range parameter.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值