gpt4 book ai didi

python - 如何将缺失的点绘制成一个完整的圆圈?

转载 作者:行者123 更新时间:2023-12-04 08:36:34 27 4
gpt4 key购买 nike

我有9个温度点。 1 个在中心,8 个在圆圈上。我需要在一个圆圈中创建一个热图。我设置了执行计算的点,并使用 scipy.interpolate.griddata,但没有绘制完整的圆,程序绘制了一个八边形。我如何填写缺失的部分?
current result

import scipy.interpolate
import numpy
import matplotlib
import matplotlib.pyplot as plt
import math

# close old plots
plt.close("all")

# some parameters
xy_center = [2,2] # center of the plot
radius = 2 # radius

# mostly original code
meanR = [33.9, 34.2, 33.1, 33.5, 33., 32.7, 32.3, 31.8, 35.]

x = numpy.array([2, 2, 2+math.sqrt(2), 4, 2+math.sqrt(2), 2, 2+(-math.sqrt(2)), 0, 2+(-math.sqrt(2))])
y = numpy.array([2, 4, 2+math.sqrt(2), 2, 2+(-math.sqrt(2)), 0, 2+(-math.sqrt(2)), 2, 2+math.sqrt(2)])

z = meanR
xi, yi = numpy.mgrid[x.min():x.max():500j, y.min():y.max():500j]

zi = scipy.interpolate.griddata((x, y), z, (xi, yi), method='cubic')

# make figure
fig = plt.figure(figsize=(10, 10))

# set aspect = 1 to make it a circle
ax = fig.add_subplot(111, aspect = 1)

# use different number of levels for the fill and the lines
CS = ax.contourf(xi, yi, zi, 300, cmap=plt.cm.viridis, zorder=1)

# make a color bar
cbar = fig.colorbar(CS, ax=ax)

# add the data points
ax.scatter(x, y, marker = 'o', c = 'b', s = 15, zorder = 3)

for i in range(9):
ax.annotate(str(z[i]), (x[i],y[i]))

# draw a circle
circle = matplotlib.patches.Circle(xy = xy_center, radius = radius, edgecolor = "k", facecolor = "none")
ax.add_patch(circle)

# remove the ticks
ax.set_xticks([])
ax.set_yticks([])

# set axes limits
ax.set_xlim(-0.5, 4.5)
ax.set_ylim(-0.5, 4.5)
plt.show()

最佳答案

径向基函数 (Rbf) 可用于内插/外推您的数据。
scipy.interpolation这是一个修改后的代码,可以生成您需要的图。

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import math
from scipy.interpolate import Rbf

# some parameters
xy_center = [2,2] # center of the plot
radius = 2 # radius

# Data part
# ---------
# mostly original code
meanR = [33.9, 34.2, 33.1, 33.5, 33., 32.7, 32.3, 31.8, 35.] #9 points data
x = np.array([2, 2, 2+math.sqrt(2), 4, 2+math.sqrt(2), 2, 2+(-math.sqrt(2)), 0, 2+(-math.sqrt(2))])
y = np.array([2, 4, 2+math.sqrt(2), 2, 2+(-math.sqrt(2)), 0, 2+(-math.sqrt(2)), 2, 2+math.sqrt(2)])
z = meanR

# use RBF (Radial basis functions) that allows extrapolation
rbf = Rbf(x, y, z, epsilon=radius+1) #epsilon is based on some parameters of the data

# Interpolation/extrapolation
# ---------------------------
xi, yi = np.mgrid[x.min():x.max():500j, y.min():y.max():500j]
# applies and get inter/extra-polated values
zi = rbf(xi, yi)

# make zi outside circle --> np.none
midr,midc = zi.shape[0]/2, zi.shape[1]/2
for er in range(zi.shape[0]):
for ec in range(zi.shape[1]):
if np.abs(math.sqrt((er-midr)**2 + (ec-midc)**2))>zi.shape[0]/2:
# outside the circle, dont plot this pixel
zi[er][ec] = np.nan
pass
pass

# make figure
fig = plt.figure(figsize=(8, 8))

# set aspect = 1 to make it a circle
ax = fig.add_subplot(111, aspect = 1)

# add the data points
ax.scatter(x, y, marker = 'o', c = 'b', s = 15, zorder = 3)

for i in range(9):
ax.annotate(str(z[i]), (x[i],y[i]))

# draw a circle
circle = matplotlib.patches.Circle(xy = xy_center, radius = radius, edgecolor = "k", facecolor = "none")
ax.add_patch(circle)

CS = ax.contourf(xi, yi, zi, 300, cmap=plt.cm.viridis, zorder=1)
cbar = fig.colorbar(CS, ax=ax, shrink=0.7) # make a color bar

# remove the ticks
ax.set_xticks([])
ax.set_yticks([])

# set axes limits
ax.set_xlim(-0.5, 4.5)
ax.set_ylim(-0.5, 4.5)
plt.show()
结果:
enter image description here

关于python - 如何将缺失的点绘制成一个完整的圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64767967/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com