Python中pandas表的散点图

2024-05-15 17:54:30 发布

您现在位置:Python中文网/ 问答频道 /正文

新学员学习python,目前正在努力完成一项任务。我试图从熊猫表中的一些数据中发布散点图,但似乎无法计算出来

以下是我的数据集示例:

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)

我试图在住房年龄的数据上画一个散点图,但是有一些困难

最初尝试将x轴作为“住房数据”,将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的大小必须相同

注:“住房数据”中的数据范围为1-53年

我想这应该是一件很容易的事,但由于某种原因,我想不出来

有人有什么建议吗


Tags: of数据代码import示例pandasdataax
1条回答
网友
1楼 · 发布于 2024-05-15 17:54:30

我知道你开始这么困惑是很常见的。请容忍我

根据您的描述,看起来您交换了xy

# 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')

enter image description here

一般来说,如果你有一个观察列表,并且你想在许多类别中对它们进行计数,那么它被称为直方图。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]

enter image description here

您还可以显式设置存储箱: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]

enter image description here

相关问题 更多 >