i have converted a csv file into nc file and tried to map it with python cartophy but it is showing an error that the temperature data is 1D. how can i solve this
i have tried some codes to create a geographic map and expected to show the temperature variations of this region but it only showing the map without the temperature data.
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from scipy.interpolate import griddata
# Load the reference CSV file into a DataFrame
reference_file_path = "C:/Users/ACER/Desktop/python
prac/meghna_pcp_station.csv"
reference_df = pd.read_csv(reference_file_path)
# Create an empty DataFrame to store the merged data
merged_df = pd.DataFrame()
# Get the list of CSV files in the directory
directory_path = "C:/Users/ACER/Desktop/python prac"
csv_files = [file for file in os.listdir(directory_path) if
file.endswith(".csv")]
# Iterate over the CSV files, read each file into a DataFrame, merge
it with the reference DataFrame based on the "name" column,
# and append it to the merged_df DataFrame
for csv_file in csv_files:
file_path = os.path.join(directory_path, csv_file)
NAME = os.path.splitext(csv_file)[0] # Extract the name from the
file
data_df = pd.read_csv(file_path)
data_df["NAME"] = NAME
merged_data = pd.merge(data_df, reference_df, on="NAME",
how="inner")
merged_df = merged_df.append(merged_data, ignore_index=True)
# Create a figure and axis for the plot
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw={"projection":
ccrs.PlateCarree()})
# Add map features
ax.coastlines()
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.LAND, facecolor="lightgray")
ax.add_feature(cfeature.OCEAN, facecolor="white")
# Plot the temperature distribution on the map
#sc = ax.scatter(merged_df["LONGITUDE"], merged_df["LATITUDE"],
c=merged_df["Temperature"], cmap="coolwarm", s=10,
edgecolors="black")
# Define the latitude and longitude values
lat = merged_df["LONGITUDE"].values
lon = merged_df["LATITUDE"].values
# Define the temperature values
temperature = merged_df["Temperature"].values
# Create a regular grid for interpolation
lon_grid, lat_grid = np.meshgrid(np.linspace(min(lon), max(lon),
100), np.linspace(min(lat), max(lat), 100))
# Interpolate the temperature values onto the grid
temperature_grid = griddata((lon, lat), temperature, (lon_grid,
lat_grid), method="linear")
vmin = min(temperature)
vmax = max(temperature)
# Plot the temperature data as a heatmap
heatmap = ax.imshow(temperature_grid, extent=[min(lon), max(lon),
min(lat), max(lat)], cmap="warm", origin="lower", aspect="auto")
# Plot the temperature distribution on the map
#cf = ax.contourf(lon, lat, temperature, cmap="coolwarm")
# Add a colorbar
#cbar = plt.colorbar(sc, label="Temperature")
cbar = plt.colorbar(heatmap, label="Temperature")
# Set plot title
plt.title("Temperature Distribution")
# Show the plot
plt.show()