Python 带Pythyon的风险矩阵
我正在尝试使用Python做一个风险矩阵,它集成了严重性和概率,我已经尝试使用热图,这可能是我迄今为止发现的最接近的一种图形,但我认为它并不代表风险矩阵的基本结构。下一幅图显示了我想要的矩阵图。我会很感激任何建议:图书馆,链接…任何能够绘制风险矩阵的东西 这是我试图在风险矩阵中找到的数据:Python 带Pythyon的风险矩阵,python,python-3.x,matplotlib,seaborn,Python,Python 3.x,Matplotlib,Seaborn,我正在尝试使用Python做一个风险矩阵,它集成了严重性和概率,我已经尝试使用热图,这可能是我迄今为止发现的最接近的一种图形,但我认为它并不代表风险矩阵的基本结构。下一幅图显示了我想要的矩阵图。我会很感激任何建议:图书馆,链接…任何能够绘制风险矩阵的东西 这是我试图在风险矩阵中找到的数据: |---------------------|------------------|----------------------| | Component | KPI
|---------------------|------------------|----------------------|
| Component | KPI | Classification
|---------------------|------------------|----------------------|
| 12 | 34 | High Criticality
|---------------------|------------------|----------------------|
Start 38 High Criticality
|---------------------|------------------|----------------------|
Fusela 45 Low Criticality
|---------------------|------------------|----------------------|
Hyd 50 Medium Criticality
|---------------------|------------------|----------------------|
Damp 51 Medium Criticality
|---------------------|------------------|----------------------|
Turbine 62 High Criticality
|---------------------|------------------|----------------------|
Intercon 65 Medium Criticality
|---------------------|------------------|----------------------|
Main Rotor 90 High Criticality
|---------------------|------------------|----------------------|
AM-19 93 High Criticality
|---------------------|------------------|----------------------|
Main Trans 98 High Criticality
|---------------------|------------------|----------------------|
这是我已经用热图实现的代码:
import matplotlib.pyplot as plt
data = data.sort_values(by = 'KPI', ascending = False)
x = 1
for element in list(data['Componente']):
data['Componente'] = data['Componente'].str.replace(str(element),'{}.
{}'.format(str(x),element))
x = x + 1
data['Clasificación'] = data['Clasificación'].str.replace('Criticidad
Alta','1. Criticidad Alta').str.replace('Criticidad Media','2. Criticidad
Media').str.replace('Criticidad Baja', '3. Criticidad Baja')
result = data.pivot(index='Componente',columns= 'Clasificacion', values =
'KPI')
sb.heatmap(result, annot= True ,cmap='RdYlGn' ,fmt=".1f", vmax=100)
plt.figtext(.5,.9,'RESULTADO MATRIZ RIESGO', fontsize=14, ha='center')
plt.show()
我想要的输出类似于下一个成像仪:
以下是plt.imshow和plt.annotate的创意:
# function to make risk matrix
def make_risk_matrix(shape=3,levels=3):
matrix = np.zeros((shape, shape))
for level in range(levels):
matrix[np.triu_indices(shape, level)] += 1
return matrix
# specify bin borders and respective positions
likelihood_bins = [100,86,60]
positions = [0,1,2]
for position, likelihood in zip(positions, likelihood_bins):
df.loc[df['KPI']<=likelihood, 'y'] = position
# generate x-positions from classification column
df['x'] = df['Classification'].replace({'High':2, 'Medium':1, 'Low':0})
# default offset for x -position
x_offset = -.4
# generate risk matrix and display as image
risk_matrix = make_risk_matrix()
plt.imshow(risk_matrix, cmap='RdYlGn_r')
# write individual components on it
# as some components will end up in hte same bin,
# caculate y-offset on the fly
for group in df.groupby(['x', 'y']):
y_offset = -.3
for ix, row in group[1].iterrows():
plt.annotate(
row['Component'],
xy=(
row['x']+x_offset,
row['y']+y_offset
)
)
y_offset +=.15 # update y_offset
plt.xlabel('Consequence')
plt.ylabel('Likelihood')
以下是plt.imshow和plt.annotate的创意:
# function to make risk matrix
def make_risk_matrix(shape=3,levels=3):
matrix = np.zeros((shape, shape))
for level in range(levels):
matrix[np.triu_indices(shape, level)] += 1
return matrix
# specify bin borders and respective positions
likelihood_bins = [100,86,60]
positions = [0,1,2]
for position, likelihood in zip(positions, likelihood_bins):
df.loc[df['KPI']<=likelihood, 'y'] = position
# generate x-positions from classification column
df['x'] = df['Classification'].replace({'High':2, 'Medium':1, 'Low':0})
# default offset for x -position
x_offset = -.4
# generate risk matrix and display as image
risk_matrix = make_risk_matrix()
plt.imshow(risk_matrix, cmap='RdYlGn_r')
# write individual components on it
# as some components will end up in hte same bin,
# caculate y-offset on the fly
for group in df.groupby(['x', 'y']):
y_offset = -.3
for ix, row in group[1].iterrows():
plt.annotate(
row['Component'],
xy=(
row['x']+x_offset,
row['y']+y_offset
)
)
y_offset +=.15 # update y_offset
plt.xlabel('Consequence')
plt.ylabel('Likelihood')
请张贴您的密码和密码data@seralok嗨,现在我加载数据和code@QuangHoang完成!现在还不清楚你希望矩阵是什么样子?就您的数据而言,x轴、y轴、每个单元格上都有什么?数据的哪一部分决定了可能性?请发布您的代码并data@seralok嗨,现在我加载数据和code@QuangHoang完成!现在还不清楚你希望矩阵是什么样子?x轴,y轴,每个单元格上的数据是什么?数据的哪一部分决定了可能性?这就是我需要的,我只需要更改轴的名称,你知道这是否可能吗?谢谢你的时间和代码这正是我需要的这是我需要的,我只需要更改轴的名称,你知道这是否可能吗?谢谢你的时间和代码,这正是我需要的