【问题标题】:DataFrame to scatter chart only plots 2 pointsDataFrame 到散点图仅绘制 2 个点
【发布时间】:2018-07-21 22:17:19
【问题描述】:

我有一个dataframe,它以时间为键,以纬度、经度为值,它可以包含 0 或位置:

Index  Latitude     Longitude
00:00  0.0          0.0
00:01  -23.4649225  -46.6964685
00:02  -23.4650154  -46.6965732
...
23:58  0.0          0.0
23:59  0.0          0.0

当我尝试绘制散点图时,它只给了我两点 scatterplottemp

我尝试将其转换为两个不同的 listsseries,但没有结果,我知道我可以将它们转换为 dictionary 并得到我的结果,但我宁愿不这样做。

plt.scatter(df['Latitude'],df['Longitude'])
plt.show()

【问题讨论】:

  • 你确定数据框实际上有两个以上的点吗?从您发布的 sn-p 中,我只能看到 (0.0, 0.0) 和 (-23.46, -46.696) 周围的两个点,这可能会在您的散点图上完全重叠
  • 也许所有点都在 (-23, -46) 的窄边距内,因为从另一点 (0,0) 缩放,它们看起来是一个点。你试过在没有 (0,0) 的情况下绘制它吗?
  • 有,1000以上是位置,其余为0
  • @Piinthesky 这个问题我已经想到了,但我不知道如何解决它......位置 0.0 是必要的
  • @Piinthesky 是的!效果很好,谢谢,但你知道绕过 0 的任何方法吗?

标签: python python-2.7 pandas matplotlib


【解决方案1】:

我有一个数据框,它以时间为键,以纬度、经度为值,它可以包含 0 或位置:

Index  Latitude     Longitude
00:00  0.0          0.0
00:01  -23.4649225  -46.6964685
00:02  -23.4650154  -46.6965732
...
23:58  0.0          0.0
23:59  0.0          0.0

当我尝试绘制散点图时,它只给了我两点 scatterplottemp

我尝试将其转换为两个不同的列表和系列,但没有结果,我知道我可以将它们转换为字典并得到我的结果,但我宁愿不这样做。

plt.scatter(df['Latitude'],df['Longitude'])
plt.show()

@Piinthesky 提供的解决方案:

过滤0

df = df[(df['Latitude'] != 0) | (df['Longitude'] != 0)]
df.plot.scatter(x = 'Latitude',y = 'Longitude')

【讨论】:

  • 如果您必须包含第二个点云(例如,确实分散在 (0, 0) 周围的点,需要在同一张图中显示),您可以调整 matplotlib example for a broken axis跨度>
  • Err,这在我的解释器中是无效的语法。我会使用df = df[(df['Latitude'] != 0) | (df['Longitude'] != 0)]
  • 运算符<> 在我的设置中仅适用于相当于!= 的Python 2.x,在Python 3 中它会引发语法错误。 Get ready for the future!
猜你喜欢
  • 2020-10-18
  • 2021-05-16
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多