环境
python:python-3.12.0-amd64
包:
matplotlib 3.8.2
pandas 2.1.4
openpyxl 3.1.2
scipy 1.12.0
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
from matplotlib.colors import ListedColormap
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import font_manager
from matplotlib.image import imread
from matplotlib.widgets import Button
from tkinter import messagebox
#解决中文乱码问题
plt.rcParams['font.sans-serif']=['kaiti']
plt.rcParams["axes.unicode_minus"]=False #解决图像中的"-"负号的乱码问题
#实心圆柱体代码1
def map_rate(X: list, to_min: float, to_max: float) -> list:
"""区间映射
Attribute:
- X: 需要映射的列表
- to_min: 要映射到的最小值
- to_max: 要映射到的最大值
"""
x_min = min(X)
x_max = max(X)
return list([int(round(to_min + ((to_max - to_min) / (x_max - x_min)) * (i - x_min), 1)) for i in X])
# rainbow色带
def rainbow(x):
# rainbow色带
#data = [(255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0)]
data = [(175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230), (175, 210, 230)]
co = map_rate(x, 0, 175)
return np.array(list(data[i] for i in co))
# 求中点
def midpoints(x):
sl = ()
for i in range(x.ndim):
x = (x[sl + np.index_exp[:-1]] + x[sl + np.index_exp[1:]]) / 2.0
sl += np.index_exp[:]
return x
# 归一化函数
def normalize(data):
mx = np.max(data) * np.ones(data.shape)
mn = np.min(data) * np.ones(data.shape)
return (data - mn) / (mx - mn)
# 定义应力与半径的关系
def Mises(r):
return np.round(r * 2, 2) # 计算von mises应力,并保留小数点后两位
#实心圆柱体代码结束
# 创建自定义颜色调色板
def create_custom_colormap(name, colors):
colors = np.array(colors)
cmap = plt.get_cmap(name)
cmap.set_over(colors[-1])
cmap.set_under(colors[0])
cmap.set_bad(colors[0])
return cmap
# 定义一些颜色
#colors = ['red', 'blue', 'green', 'yellow', 'purple']
colors = ['red', 'orange', 'yellow', 'green', 'blue']
# 创建自定义颜色映射对象
my_colormap = create_custom_colormap('turbo', colors)
# 读取Excel文件
df = pd.read_excel('煤仓模拟参数.xlsx')
#df = pd.read_excel('煤仓模拟参数222.xlsx')
#去除无效点
# 根据A列和B列分组,并将每组中C列的值更改为该组中C列的最小值
df['Z轴'] = df.groupby(['X轴', 'Y轴'])['Z轴'].transform('min')
#print('数量:',df)
# 将DataFrame导出到Excel文件
#df.to_excel('u.xlsx', index=False)
# 提取x、y、z数据
x = df['X轴'].values
y = df['Y轴'].values
z = df['Z轴'].values
plt.rcParams['figure.facecolor'] = 'lightblue'
# 创建三维坐标轴对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 设置figure标题
# 使用平滑曲面插值方法创建地形图(假设使用样条插值方法)
#smoothed_terrain = ax.scatter(x, y, z, cmap='viridis')
# 使用griddata函数进行插值,这里使用最近邻插值法,你也可以选择其他的插值方法
# 插值后的数据用于绘制平滑曲面地形图
grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j]
grid_z = griddata((x, y), z, (grid_x, grid_y), method='cubic')
# 设置颜色映射和透明度
cmap = plt.get_cmap('RdYlBu') # 选择颜色映射
norm = plt.Normalize(vmin=-5, vmax=5) # 标准化高度值
alpha = norm(grid_z).data # 计算透明度
colors = cmap(norm(grid_z).data) # 计算颜色值
# 使用平滑曲面插值后的数据绘制地形图
# 绘制地形图(camp:coolwarm,viridis,plasma,inferno,magma,cividis,rainbow)
# 绘制地形图(camp:coolwarm,viridis,plasma,inferno,magma,cividis,rainbow)
cmap = ListedColormap(['blue', 'green', 'yellow', 'orange','Red'])
ax.contourf(grid_x, grid_y, grid_z, levels=300, cmap=my_colormap)
#ax.contourf(grid_x, grid_y, grid_z, levels=60, cmap='viridis')
#实心圆柱体代码2
# 定义圆柱的参数
R = 9000 # 圆柱的半径
# 查找列'X'的绝对值等于9000的行,并获取列'Z'中的最小值
H= df[(abs(df['X轴']) == 9000) & (df['Z轴'].notnull())]['Z轴'].min() # 圆柱的高
#H = df[(abs(df['X轴']) == 9000) & (df['Z轴'].notnull())]['Z轴'].max() # 圆柱的高
#H=36316
# 网格点数量
nr = 19j # 沿半径分几层
ntheta = 25j # >=4
nh = 9j
# 转换坐标系,并求中点
nr, theta, nz = np.mgrid[0:R:nr, 0:np.pi * 2:ntheta, 16452:H:nh]
nx = nr * np.cos(theta)
ny = nr * np.sin(theta)
rc, thetac, zc = midpoints(nr), midpoints(theta), midpoints(nz)
# 填充网格
a, b, c = rc.shape
rr = list(rc[:, 0, 0])
sphere = np.zeros((a, b, c)) == 0
# 设置颜色
hsv = np.zeros(sphere.shape + (3,))
r_color1 = rainbow(rr)
r_color2 = normalize(r_color1)
rgb_r = r_color2[:, 0]
rgb_g = r_color2[:, 1]
rgb_b = r_color2[:, 2]
for i in range(a):
hsv[i, ..., 0] = rgb_r[i] * np.ones((b, c))
hsv[i, ..., 1] = rgb_g[i] * np.ones((b, c))
hsv[i, ..., 2] = rgb_b[i] * np.ones((b, c))
# 求应力
mises_r = np.linspace(0, R, a)
mises = Mises(mises_r)
ax.voxels(nx, ny, nz, sphere,
facecolors=hsv,
edgecolors=np.clip(2 * hsv - 0.5, 0, 1),
linewidth=0.5)
#实心圆柱体代码2结束
#实心圆柱上面在套一个白色泉
# 先根据极坐标方式生成数据
u1 = np.linspace(0, 2 * np.pi, 50) # 把圆分按角度为50等分
h1 = np.linspace(16452, 36316, 20) # 把高度9000均分为20份
x1 = np.outer(np.sin(u1), np.ones(len(h1))*9000) # x值重复20次
y1 = np.outer(np.cos(u1), np.ones(len(h1))*9000) # y值重复20次
z1 = np.outer(np.ones(len(u1)), h1) # x,y 对应的高度
# Plot the surface
ax.plot_surface(x1, y1, z1, cmap=plt.get_cmap('autumn'),alpha=0.15)
ax.grid(True)
# 添加颜色条
cbar = plt.colorbar(plt.imshow(grid_z, cmap=cmap), ax=ax)
cbar.set_label('Height')
# 读取背景图
img = imread('1.jpeg')
# 添加背景图
ax.imshow(img, alpha=0.5)
# 设置x轴的刻度间隔
ax.set_xticks(np.arange(-9000, 9000, 2500)) # 从-7500到7500,步长为2500
# 设置y轴的刻度间隔
ax.set_yticks(np.arange(-9000, 9000, 2500)) # 从-7500到7500,步长为2500
# 设置z轴的刻度间隔
#ax.set_zticks(np.arange(16452, 36316, 2500)) # 从10000到31000,步长为2500
# 创建包含不规则刻度的数组
z_ticks = np.array([16452,18952,21452,23952,26452,28952,31452,33952,36316])
# 设置z轴刻度间隔
ax.set_zlim([16452, 36316]) # 设置z轴的范围
ax.set_zticks(z_ticks) # 设置z轴刻度的值
# 设置新的刻度列表
ax.set_zticks(z_ticks) # 设置新的刻度列表
# 设置x轴和y轴的标签为空字符串,并隐藏它们
ax.set_xlabel('')
ax.set_ylabel('')
ax.set_xticks([])
ax.set_yticks([])
# 设置坐标轴的位置和方向
ax.spines['right'].set_color('none') # 隐藏右侧的坐标轴线
ax.spines['top'].set_color('none') # 隐藏顶部的坐标轴线
ax.spines['bottom'].set_color('none') # 隐藏右侧的坐标轴线
ax.spines['left'].set_color('none') # 隐藏顶部的坐标轴线
#计算面积,容积,最高料位等
h = df['Z轴'].mean()-16452
#print(h)
# 计算圆柱体的体积
#pi = np.pi
#V = np.pi * r**2 * h # 圆柱体体积公式:πr²h r 9000 h-16452 983.6 3000上下就是对的
#print(V)
# 计算圆柱体的体积
r=9000
pi = np.pi
V = np.pi * r**2 * h # 圆柱体体积公式:πr²h r 9000 h-16452 983.6 3000上下就是对的
#print('V=',V)
def mm3_to_m3(mm3):
m3 = mm3 / (1000**3)
return m3
# 测试代码
mm3_value = V # 1立方米等于1000000立方毫米
m3_value = mm3_to_m3(mm3_value)
print(m3_value)
m3_value_1=m3_value+983.6
print('体积=',m3_value_1)
zl=1.5*m3_value_1
print('质量=',zl)
VP=m3_value_1/6022.72#6022.72为总桶的总体积
print('容积=',VP)
# 找到该列的最大值和最小值
max_value = df['Z轴'].max()
min_value = df['Z轴'].min()
h=h+16452
# 打印结果
print("最高料位=",max_value)
print("最低料位=",min_value)
print("平均料位=",h)
# 添加标题和坐标轴标签
ax.set_title('高度变化显示顶仓、筒和底仓的料的变化')
# 在图形上添加文本
str = "体积="+np.array2string(m3_value_1)+"\n质量="+np.array2string(zl)+"\n容积="+"{:.2%}".format(VP)+"\n最高料位="+np.array2string(max_value)+"\n最低料位="+np.array2string(min_value)+"\n平均料位="+np.array2string(h)
#ax.text(-25000,-5000,15000,str)
ax.text2D(-0.4, 0.95, str, transform=ax.transAxes,va='top')#, va='top'
print(str)
# 改变图形显示的角度
ax.view_init(elev=30, azim=-86)
# 设置图形比例,使X、Y轴和面板底部重合
ax.set_aspect('equal', adjustable='box')
# 设置图形比例,使X、Z轴重合
ax.set_axis_off() # 关闭坐标轴
plt.show()
资源下载(分享-->资源分享):
链接:https://pan.baidu.com/s/1UlP0lsma8OWchfV5kstEFQ
提取码:kdgr