直方图的类型错误

时间:2017-10-20 03:58:08

标签: python pandas numpy matplotlib data-visualization

我正在尝试使用此问题中的Joe Kington代码Matplotlib - label each bin

但是当我运行我的代码时,收到错误消息

len() of unsized object

为行

counts, bins, patches = ax.hist(data, facecolor='yellow', edgecolor='gray') 

有人知道这个的潜在原因吗?

数据是几百行和两列浮点数。

from matplotlib.ticker import FormatStrFormatter

data = df[df.columns[1:]]
print(data)
fig, ax = plt.subplots()
counts, bins, patches = ax.hist(data, facecolor='yellow', edgecolor='gray')

# Set the ticks to be at the edges of the bins.
ax.set_xticks(bins)
# Set the xaxis's tick labels to be formatted with 1 decimal place...
ax.xaxis.set_major_formatter(FormatStrFormatter('%0.1f'))

# Change the colors of bars at the edges...
twentyfifth, seventyfifth = np.percentile(data, [25, 75])
for patch, rightside, leftside in zip(patches, bins[1:], bins[:-1]):
    if rightside < twentyfifth:
        patch.set_facecolor('green')
    elif leftside > seventyfifth:
        patch.set_facecolor('red')

# Label the raw counts and the percentages below the x-axis...
bin_centers = 0.5 * np.diff(bins) + bins[:-1]
for count, x in zip(counts, bin_centers):
    # Label the raw counts
    ax.annotate(str(count), xy=(x, 0), xycoords=('data', 'axes fraction'),
        xytext=(0, -18), textcoords='offset points', va='top', ha='center')

    # Label the percentages
    percent = '%0.0f%%' % (100 * float(count) / counts.sum())
    ax.annotate(percent, xy=(x, 0), xycoords=('data', 'axes fraction'),
        xytext=(0, -32), textcoords='offset points', va='top', ha='center')


# Give ourselves some more room at the bottom of the plot
plt.subplots_adjust(bottom=0.15)
plt.show()

print(数据)给了我这个:

0 1490.68 967.20 
1 1485.69 978.55 
2 1286.84 512.02 
3 1257.85 858.85 
4 1257.17 693.87 

5 1288.44 591.80 ...

0 个答案:

没有答案