首页 > 解决方案 > Seanborn:在 scatterplor 上绘制第三个变量

问题描述

我有一个按条件过滤的数据框:

df2 = df[['longitude', 'latitude', 'price']]
df_Scatter = df2[df2['price']>150]

df_Scatter 现在显示为:

    longitude   latitude    price
1   4.89354 52.36509    236
6   4.84838 52.35132    219
7   4.88321 52.37891    160
8   4.88932 52.37537    211
13  4.86103 52.36266    250
... ... ... ...
18482   4.87564 52.37167    176
18496   4.88266 52.37398    250
18516   4.92186 52.35830    275
18517   4.91443 52.36978    330
18518   4.87967 52.36404    350
6288 rows × 3 columns

我想绘制longitudelatitude作为坐标和编码:价格到绘图的大小和颜色。

这就是我到目前为止所拥有的。

plt.scatter(df_Scatter.longitude, df_Scatter.latitude, c=df_Scatter.price, s = df_Scatter.price)
plt.show()

返回两个错误

AttributeError: 'int' object has no attribute 'sqrt'

TypeError: loop of ufunc does not support argument 0 of type int which has no callable sqrt method

和一个空图,其中轴范围为 0-1。

是否由于数据类型的不同而返回以下错误? 我瞄准的示例图在其图表上有一个图例,其中当编码为图的颜色和大小时,价格仍为 INT 格式。

标签: pythonmatplotlibseaborn

解决方案


尝试seaborn

import seaborn as sns
import matplotlib.pyplot as plt

ax = sns.scatterplot(x=df['longitude'], y=df['latitude'], size = df['price'], hue= df['price'])

输出:

在此处输入图像描述


推荐阅读