admin管理员组

文章数量:1395274

I have a dataset from a disdrometer containing the following variables:

  • Time stamp (LST) – Local Standard Time
  • Rainrate – Rain rate in mm/hr
  • Mass weighted mean D (mm) – Mass-weighted mean diameter of raindrops
  • log N(D) – Logarithm of the drop size distribution

I want to visualize this data as a heatmap time series similar to the following sample figure,

where: The x-axis represents time (timestamps). The y-axis represents either Rainrate and log N(D). The color intensity represents log N(D).

How can I efficiently create this heatmap time series in Python? Any example code would be greatly appreciated! The sample data is also attached here looks like :

Time stamp (LST),Disdro RR (mmhr),Mass weighted mean D (mm),log N(D)
900,0,0,0
905,0,0,0
910,0,0,0
915,0,0,0
920,0,0,0
925,0,0,0
930,0,0,0
935,0,0,0
940,0,0,0
945,0,0,0
950,0,0,0
955,0,0,0
1000,0,0,0
1005,0,0,0
1010,0,0,0
1015,0,0,0
1020,0,0,0
1025,0,0,0
1030,0,0,0
1035,0,0,0
1040,0,0,0
1045,0,0,0
1050,0.583,0.67,1.94
1055,0.435,0.64,1.19
1100,0.141,0.748,1.74
1105,0.593,0.88,2.13
1110,1.339,0.87,1.82
1115,1.42,0.72,2.17
1120,3.615,0.85,2.79
1125,3.51,0.961,2.69
1130,4.75,0.82,2.56
1135,4.61,0.85,2.79
1140,5.546,0.95,2.96
1145,6.052,1.03,3.06
1150,7.18,0.856,3.28
1155,7.83,1.23,2.88
1200,13.21,1.382,2.85

I did try to do this with the initial code, but I didn't manage to reproduce the sample figure correctly. (Why is the y axis "inverted"?)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Load your data
file_path = "07292024_Disdrometer.csv"  # Replace with your file path
df = pd.read_csv(file_path)

    # Convert Time to HH:MM format
df["Time stamp (LST)"] = df["Time stamp (LST)"].astype(str).str.zfill(4)
df["Time (HH:MM)"] = df["Time stamp (LST)"].str[:2] + ":" + df["Time stamp (LST)"].str[2:]

# Pivot the table to prepare for heatmap
pivot_df = df.pivot(index="log N(D)", columns="Time (HH:MM)", values="log N(D)")

# Create the heatmap
plt.figure(figsize=(12, 6))
sns.heatmap(pivot_df, cmap="viridis", cbar_kws={'label': 'log N(D)'}, linewidths=0.1)

# Formatting the plot
plt.xlabel("Time (LST)")
plt.ylabel("log N(D)")
plt.title("Heatmap of Drop Size Distribution Over Time")

# Rotate x-axis labels for better readability
plt.xticks(rotation=45, ha="right")

plt.show()

which produces this figure.

本文标签: python 3xHow to create a time series heat mapStack Overflow