我要把feature_type_mean放在横轴其他位置,请增加代码,#这种初始化操作可以用来创建一个空的数据结构,用于后续存储和填充数据。在这种情况下,DataFrame的所有元素被初始化为0,准备接收进一步的数据填充。 filter_features = pd.DataFrame(0, index=filter_names, columns=feature_type_names, ) for key in c: for filter_name in filter_names: for feature_type_name in feature_type_names: if filter_name in key and feature_type_name in key: # print(filter_name, feature_type_name, key, c[key]) filter_features.loc[filter_name, feature_type_name] += c[key] filter_features['filter_mean'] = filter_features.mean(axis = 1) filter_features.loc['feature_type_mean'] = filter_features.mean(axis = 0) # %% plt.figure(figsize=(8, 12), dpi=300) sns.set_style('white', {'font.sans-serif': ['simsun', 'Times New Roman'], "size": 6}) ax = sns.heatmap(filter_features, # .apply(np.log1p), #vmin=5, vmax=17, fmt=".3f", annot=False, cmap="YlOrBr",#"vlag",#"YlOrBr", # cmap="RdBu_r", annot_kws={"size": 6}, square=True ) # label_y = ax.get_yticklabels() # plt.setp(label_y, rotation=45) # label_x = ax.get_xticklabels() # plt.setp(label_x, rotation=45) # plt.tick_params(labelsize=6) plt.show()
时间: 2024-02-25 10:56:55 浏览: 116
To move the 'feature_type_mean' column to a different position in the DataFrame, you can use the `reindex` method of pandas DataFrame. Here's the modified code:
```
filter_features = pd.DataFrame(0, index=filter_names, columns=feature_type_names)
for key in c:
for filter_name in filter_names:
for feature_type_name in feature_type_names:
if filter_name in key and feature_type_name in key:
filter_features.loc[filter_name, feature_type_name] += c[key]
filter_features['filter_mean'] = filter_features.mean(axis=1)
# Calculate the mean of each column and store it in a new DataFrame
feature_type_mean = filter_features.mean(axis=0)
feature_type_mean_df = pd.DataFrame(feature_type_mean, columns=['feature_type_mean'])
# Reorder the columns in the DataFrame
filter_features = pd.concat([feature_type_mean_df, filter_features.drop(columns=['feature_type_mean'])], axis=1)
plt.figure(figsize=(8, 12), dpi=300)
sns.set_style('white', {'font.sans-serif': ['simsun', 'Times New Roman'], "size": 6})
ax = sns.heatmap(filter_features, fmt=".3f", annot=False, cmap="YlOrBr", annot_kws={"size": 6}, square=True)
plt.show()
```
In this modified code, the `feature_type_mean` column is calculated separately and stored in a new DataFrame `feature_type_mean_df`. Then, the `concat()` method is used to combine this DataFrame with the original `filter_features` DataFrame, but with the columns reordered. Finally, the heatmap is plotted using the modified `filter_features` DataFrame.
阅读全文