我试着画出四个不同的图表,显示四种不同类型的潮汐。有不同的振幅和相移。
import numpy as np
import matplotlib.pyplot as plt
# Define tidal constituents from Table 4.1
constituents = [
{"symbol": "M2", "period": 12.42, "relative_amplitude": 100.0},
{"symbol": "S2", "period": 12.00, "relative_amplitude": 46.6},
{"symbol": "K1", "period": 23.93, "relative_amplitude": 58.4},
{"symbol": "O1", "period": 25.82, "relative_amplitude": 41.5},
]
# Define the time series for one month (31 days, 24 hours per day)
days = 31
hours_per_day = 24
total_hours = days * hours_per_day
time = np.linspace(0, total_hours, total_hours, endpoint=False) # Hours
# Initialize arrays for each tidal component
tide_components = {constituent["symbol"]: np.zeros(total_hours) for constituent in constituents}
# Calculate amplitudes and phase shifts for each constituent
for constituent in constituents:
amplitude = constituent["relative_amplitude"] / 100.0 # Normalize amplitude
period = constituent["period"]
phase_shift = np.random.uniform(0, 2 * np.pi) # Random phase shift in radians
tide_components[constituent["symbol"]] = amplitude*np.cos(2*np.pi / period * time - phase_shift)
# Sum all tidal components to create the final tide
total_tide = np.sum(list(tide_components.values()), axis=0)
# Plot the tidal chart
plt.figure(figsize=(12, 6))
plt.plot(time, total_tide)
plt.title("Tidal Chart for a Month")
plt.xlabel("Time (hours)")
plt.ylabel("Water Level")
plt.grid(True)
# Display the plot
plt.show()
# Display the ratios for each location
ratios = {
"Location 1": (tide_components["K1"] + tide_components["O1"]) / (tide_components["M2"] + tide_components["S2"]),
"Location 2": (tide_components["K1"] + tide_components["O1"]) / (tide_components["M2"] + tide_components["S2"]),
"Location 3": (tide_components["K1"] + tide_components["O1"]) / (tide_components["M2"] + tide_components["S2"]),
"Location 4": (tide_components["K1"] + tide_components["O1"]) / (tide_components["M2"] + tide_components["S2"]),
}
# Display the ratios table
print("Ratios for Tide Plots:")
print("Location\t(K1+O1)/(M2+S2)")
for location, ratio in ratios.items():
print(f"{location}\t\t{ratio:.4f}")
我想画一张图表来显示不同的潮汐和比率。
我一直得到这个错误,我不知道如何修复它:
Ratios for Tide Plots:
Location (K1+O1)/(M2+S2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-0cd21def11d7> in <module>
43 print("Location\t(K1+O1)/(M2+S2)")
44 for location, ratio in ratios.items():
---> 45 print(f"{location}\t\t{ratio:.4f}")
TypeError: unsupported format string passed to numpy.ndarray.__format__
1条答案
按热度按时间fkaflof61#
在您的代码中,
ratio
是一个NumPy数组。您可以使用np.array2string
化NumPy数组以进行字符串打印:更新后,您的绘图和输出将打印出来,没有错误: