You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:含time键的DataFrame绘制散点图仅显示2个点

Hey there! Let's dig into why your scatter plot is only showing 2 points and fix this without converting to a dictionary. Here are some targeted solutions based on common issues with this scenario:

Fixing the 2-Point Scatter Plot Issue (No Dictionary Conversion Needed)

First, let's narrow down the core problems—most likely, your DataFrame has lots of 0 values that are cluttering or masking valid points, or there's a data type/index issue throwing off the plot. Let's tackle this step by step:

1. Filter out invalid 0 values first

If those 0s in lat and lon are placeholder values for invalid locations, filter them out before plotting to focus on real data points:

import matplotlib.pyplot as plt
import pandas as pd

# Keep only rows where both lat and lon are not 0
filtered_df = df[(df['lat'] != 0) & (df['lon'] != 0)]

# Plot the cleaned data
scatterplottemp = plt.scatter(filtered_df['lon'], filtered_df['lat'])
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()

This ensures you're only plotting meaningful location data, not the placeholder zeros.

2. Verify and fix data types

Sometimes lat/lon might be stored as strings or non-numeric types, which can break plotting. Check and convert them if needed:

# Check current data types
print(df[['lat', 'lon']].dtypes)

# Convert to numeric, forcing invalid values to NaN
df['lat'] = pd.to_numeric(df['lat'], errors='coerce')
df['lon'] = pd.to_numeric(df['lon'], errors='coerce')

# Drop rows with NaN (converted invalid values)
clean_df = df.dropna(subset=['lat', 'lon'])

# Plot again
scatterplottemp = plt.scatter(clean_df['lon'], clean_df['lat'])
plt.show()

3. Plot directly from DataFrame columns (avoid list/Series conversion)

You mentioned converting to lists/Series didn't help—try passing the DataFrame columns directly to scatter(), and add color coding to spot 0 values vs valid points:

# Plot all points, color-code valid vs 0 values
scatterplottemp = plt.scatter(
    df['lon'], 
    df['lat'], 
    c=['red' if (lat != 0 and lon != 0) else 'gray' for lat, lon in zip(df['lat'], df['lon'])],
    alpha=0.7
)
plt.legend(['Valid Points', 'Placeholder 0s'])
plt.show()

This will make it clear if you really only have 2 valid points, or if the 0s were hiding other data.

4. Check data distribution and adjust plot bounds

If you know you have more than 2 valid points but they're not showing up, check if they're overlapping or outside the default plot bounds:

# First, count how many valid points you actually have
valid_point_count = len(df[(df['lat'] != 0) & (df['lon'] != 0)])
print(f"Number of valid location points: {valid_point_count}")

# If count is higher than 2, adjust plot axes to fit all points
scatterplottemp = plt.scatter(df['lon'], df['lat'])
plt.xlim(df['lon'].min() - 0.5, df['lon'].max() + 0.5)
plt.ylim(df['lat'].min() - 0.5, df['lat'].max() + 0.5)
plt.show()

This ensures no valid points get cut off by the default axis limits.


内容的提问来源于stack exchange,提问作者Gabriel_Koch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:37:28