I am trying to create a Hovmoller plot in Python on Google Colab, but Hovmoller is not showing as it should show. It’s showing vertical different color boxes
how to fix this?
Here is the code that I am using on Google Colab:
from google.colab import files
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Upload the Excel file
uploaded = files.upload()
# Read the Excel file
df = pd.read_excel(list(uploaded.keys())[0])
# Convert the Timestamp to a datetime format
df['Timestamp'] = pd.to_datetime(df['Timestamp'], format='%Y%m%d%H%M')
# Set the Timestamp as the index
df.set_index('Timestamp', inplace=True)
# Resample the data to monthly averages
df_monthly = df.resample('M').mean()
# Create the Hovmöller plot
fig, ax = plt.subplots(figsize=(15, 5))
# Define the edges for pcolormesh
time_edges = np.linspace(mdates.date2num(df_monthly.index.min()), mdates.date2num(df_monthly.index.max()), len(df_monthly.index) + 1)
temperature_edges = np.linspace(df_monthly['Temperature'].min(), df_monthly['Temperature'].max(), len(df_monthly['Temperature']) + 1)
# Create the meshgrid for edges
X, Y = np.meshgrid(time_edges, temperature_edges)
# Create the data grid for pcolormesh
data_grid = np.tile(df_monthly['Temperature'].values, (len(df_monthly['Temperature']), 1))
# Create the plot
c = ax.pcolormesh(X, Y, data_grid, shading='auto', cmap='jet')
# Set the colorbar
plt.colorbar(c, ax=ax, label='Temperature (°C)')
# Set the x-axis to show the datetime format
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
# Set labels and title
plt.xlabel('Time')
plt.ylabel('Temperature (°C)')
plt.title('Hovmöller Plot of Monthly Average Temperature')
# Show the plot
plt.show()