【问题标题】:Make a histogram from csv data从 csv 数据制作直方图
【发布时间】:2017-12-21 22:48:59
【问题描述】:

我正在尝试使用 .CSV 文件中的数据制作直方图。我将下面的代码放在一起,运行时出现“int”对象不可迭代”错误,有什么想法吗?

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

file = "...sp histo.csv"
data = pd.read_csv(file)

year_2016 = np.array(data['2016'])
year_2017 = list(np.array(data['2017']))

small_ret = min(min(year_2016),min(year_2017))
large_ret = max(max(year_2016),max(year_2017))
bins = np.arange(small_ret, large_ret, 0.1)

plt.hist(year_2017,bins=bins, range = 20)
plt.show()

【问题讨论】:

  • 哪一行导致错误?
  • 给我们一个最小的、可复制的示例数据。这样,很难说你的错误发生在哪里!

标签: python pandas numpy matplotlib histogram


【解决方案1】:

这行有问题:

plt.hist(year_2017,bins=bins, range = 20)

根据hist 的文档字符串,范围必须是一个元组

range : tuple or None, optional
    The lower and upper range of the bins. Lower and upper outliers
    are ignored. If not provided, `range` is (x.min(), x.max()). Range
    has no effect if `bins` is a sequence.

    If `bins` is a sequence or `range` is specified, autoscaling
    is based on the specified bin range instead of the
    range of x.

【讨论】:

    猜你喜欢
    • 2012-02-03
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 2014-09-08
    • 2021-12-09
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多