2-6

陈陈陈~發表於2024-10-08

import pandas as pd
import numpy as np
x = np.arange(0, 101, 1)
y = np.arange(0, 101, 1)
z = np.random.randint(0, 1001, size=(101, 101))
df = pd.DataFrame(data=z, index=x, columns=y)
df.to_excel('data.xlsx')
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import RectBivariateSpline
df = pd.read_excel('data.xlsx', header=None)
x = df.iloc[1:, 0].values
y = df.iloc[0, 1:].values
z = df.iloc[1:, 1:].values
plt.contour(x, y, z)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Contour Plot')
plt.colorbar()
point1 = (30, 0)
point2 = (43, 30)
plt.annotate('(30,0)', point1, textcoords="offset points", xytext=(0,10), ha='center')
plt.annotate('(43,30)', point2, textcoords="offset points", xytext=(0,10), ha='center')
interp_func = RectBivariateSpline(x, y, z)
x_min, x_max = min(x), max(x)
y_min, y_max = min(y), max(y)
grid_size = 100
x_grid = np.linspace(x_min, x_max, grid_size)
y_grid = np.linspace(y_min, y_max, grid_size)
z_grid = interp_func(x_grid, y_grid)
area = np.trapz(np.trapz(z_grid, x_grid), y_grid)
print('區域面積:', area)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_excel('data2.xlsx', header=None)
x = df.iloc[1:, 0].values
y = df.iloc[0, 1:].values
z = df.iloc[1:, 1:].values
X, Y = np.meshgrid(y, x)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, z)
ax.set_xlabel('Y')
ax.set_ylabel('X')
ax.set_zlabel('Z')
plt.show()

print('3006')