import pandas as pd import matplotlib.pyplot as plt from PIL import Image import os…
- User
import pandas as pd import matplotlib.pyplot as plt from PIL import Image import os
文件路径模板
csv_template = r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\Finish-excel\north{}.csv" image_path = r"C:\Users\jack8\Dropbox\我的電腦 (LAPTOP-GNOUPD9G)\Downloads\01.png"
设置年份范围
years = range(2013, 2024)
打开并翻转图片
img = Image.open(image_path) flipped_img = img
获取图片的宽度和高度
img_width, img_height = flipped_img.size
将图片设置为绘图背景
fig, ax = plt.subplots(figsize=(60, 30)) # 调整绘图尺寸
extent 参数将图片放在 x 范围 (0, img_width) 和 y 范围 (0, img_height) 上
ax.imshow(flipped_img, extent=[0, img_width, 0, img_height])
为每个年份读取数据并绘制散点图
for year in years: # 生成当前年份的文件路径 csv_path = csv_template.format(year)
code# 检查文件是否存在 if os.path.exists(csv_path): # 读取 CSV 文件 data = pd.read_csv(csv_path) # 假设 x 和 y 列名一致 if 'x' in data.columns and 'y' in data.columns: # 将 y 坐标进行翻转处理,使其与翻转的背景图片方向一致 ax.scatter(data['x'], img_height - data['y'], label=str(year), s=50, linewidths=0.5, alpha=0.7) else: print(f"Warning: 'x' or 'y' column not found in {csv_path}") else: print(f"File not found: {csv_path}")设置 x 和 y 轴范围与图片尺寸一致
ax.set_xlim(0, img_width) ax.set_ylim(0, img_height)
添加图例
#ax.legend(title="Year")
添加标题和标签
#plt.title("Coastline Features (2013-2024)") #plt.xlabel("x") #plt.ylabel("y")
显示图形
plt.show()
##image_path = r"C:\Users\jack8\Dropbox\我的電腦 (LAPTOP-GNOUPD9G)\Downloads\01.png" 這個地方能夠改成不要圖片,一般顯示表格即可
- Assistant
好的,我已经修改了您的代码,将背景图片部分移除,并调整了代码以在没有图片背景的情况下正常显示散点图。下面是修改后的代码:
python