【问题标题】:Scatter Plot from pandas table in PythonPython中熊猫表的散点图
【发布时间】:2020-11-16 07:38:26
【问题描述】:

python 新手,目前正在努力完成一项任务。我正在尝试根据 pandas 表中的某些数据发布散点图,但似乎无法解决。

这是我的数据集的一个示例:

import pandas as pd

data = {'housing_age': [14, 11, 3, 4],
        'total_rooms': [25135, 32627, 39320, 37937],
        'total_bedrooms': [4819, 6445, 6210, 5471],
        'population': [35682, 28566, 16305, 16122]}

df = pd.DataFrame(data)

我正在尝试在 Housing_age 中的数据上绘制散点图,但很难弄清楚。

最初尝试将 x 轴设为“housing_data”,将 y 轴设为住房数据计数,但无法让代码正常工作。然后在某处读到x轴应该是可变的,y轴应该是恒定的,所以尝试了这段代码:

x='housing_data'
y=[0,5,10,15,20,25,30,35,40,45,50,55]
plt.scatter(x,y)
ax.set_xlabel("Number of buildings")
ax.set_ylabel("Age of buildings")

但是得到这个错误: ValueError: x 和 y 的大小必须相同

注意 - 'housing_data' 中的数据范围为 1-53 年。

我想这应该是一件很容易的事情,但由于某种原因我无法弄清楚。

有人有什么建议吗?

【问题讨论】:

  • x 和 y 需要是相同长度的数字列表/数组。您是否去过 matplotlib 网站并查看了包含大量示例代码的“图库”?
  • 如果你想绘制一个直方图而不是散点图的“事物计数”。在这种情况下,您只需要一个值向量。再次,查看图库中的 matplotlib 站点或有关如何美化它的文档。
  • 以后要补充资料,edit的问题。除此之外,尚不清楚您要绘制什么。查看matplotlib.pyplot.scatter,底部有示例链接。
  • 感谢@AirSquid - 现在通读。目前试图以另一种方式表示它,其中 x = house_age 和 y = house_suburb。两个数据集都是完整的,没有零字段。一个是数字,一个是字符串。我的代码是: x=data['housing_data'] y=data['ocean_proximity'] plt.scatter(x,y) ax.set_xlabel("建筑物数量") ax.set_ylabel("建筑物郊区") I'出现错误:TypeError: 'NoneType' object is not subscriptable 我认为这发生在 0 值数据上?我有点失落对不起......

标签: python pandas matplotlib scatter-plot


【解决方案1】:

我知道你刚开始,所以困惑很常见。请多多包涵。

根据您的描述,您似乎交换了x 和y:

# x is the categories: 0-5 yrs, 5-10 yrs, ...
x = [0,5,10,15,20,25,30,35,40,45,50,55]

# y is the number of observations in each category
# I just assigned it some random numbers
y = [78, 31, 7, 58, 88, 43, 47, 87, 91, 87, 36, 78]

plt.scatter(x,y)
plt.set_title('Housing Data')

一般来说,如果您有一个观察列表,并且您想在多个类别中对它们进行计数,则称为直方图。 pandas 有很多方便的功能可以让你快速查看数据。这个问题感兴趣的是hist - 创建一个直方图:

# A series of 100 random buildings whose age is between 1 and 55 (inclusive)
buildings = pd.Series(np.random.randint(1, 55, 100))

# Make a histogram with 10 bins
buildings.hist(bins=10)

# The edges of those bins were determined automatically so they appear a bit weird:
pd.cut(buildings, bins=10)

0     (22.8, 28.0]
1      (7.2, 12.4]
2     (33.2, 38.4]
3     (38.4, 43.6]
4     (48.8, 54.0]
          ...     
95    (48.8, 54.0]
96    (22.8, 28.0]
97    (12.4, 17.6]
98    (43.6, 48.8]
99    (1.948, 7.2]

您还可以明确设置 bin:0-5、5-10、...、50-55

buildings.hist(bins=range(0,60,5))

# Then the edges are no longer random
pd.cut(buildings, bins=range(0,60,5))
0     (25, 30]
1      (5, 10]
2     (30, 35]
3     (40, 45]
4     (45, 50]
        ...   
95    (45, 50]
96    (25, 30]
97    (15, 20]
98    (40, 45]
99      (0, 5]

【讨论】:

  • 感谢代码 - 这是一个很棒的演练。非常感谢您的帮助。如果我试图从 pandas 表中输入 2 个现有数据集,你知道我是如何让它工作的吗?我认为这可能有效:x=df['housing_age'] y=df['suburb'] plt.scatter(x,y) 但我收到 NoneType 错误
  • 对于散点图,matplotlib 需要 2 个数字数组:一个代表 x 坐标,另一个代表 y 坐标。如果任何一个数组都是非数字的,它将不起作用
猜你喜欢
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 2015-02-12
  • 1970-01-01
相关资源
最近更新 更多