如何在 matplotlib 中绘制热图,左右两侧都有标签

How to plot heat map in matplotlib with label at both side right and left

已更新

我已经写下了如下所示的代码..

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv("data_1.csv",index_col="Group")
print df

fig,ax = plt.subplots(1)
heatmap = ax.pcolor(df)########
ax.pcolor(df,edgecolors='k')

cbar = plt.colorbar(heatmap)##########

plt.ylim([0,12])
ax.invert_yaxis()


locs_y, labels_y = plt.yticks(np.arange(0.5, len(df.index), 1), df.index)
locs_x, labels_x = plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
ax.set_xticklabels(labels_x, rotation=10)
ax.set_yticklabels(labels_y,fontsize=10)
plt.show()

它接受如下所示的输入并绘制一个热图,左侧和底部的两侧标签..

GP1,c1,c2,c3,c4,c5
S1,21,21,20,69,30
S2,28,20,20,39,25
S3,20,21,21,44,21

我还想在数据的下方添加额外的标签,并想绘制一个带有三边标签的热图。左右和底部。

GP1,c1,c2,c3,c4,c5
S1,21,21,20,69,30,V1
S2,28,20,20,39,25,V2
S3,20,21,21,44,21,V3

我应该将哪些更改合并到代码中。

请帮忙..

您可以在图的右侧创建一个新轴,称为 twinx。然后你需要像调整第一个轴一样调整这个轴。

u = u"""GP1,c1,c2,c3,c4,c5
S1,21,21,20,69,30
S2,28,20,20,39,25
S3,20,21,21,44,21"""

import io
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df= pd.read_csv(io.StringIO(u),index_col="GP1")

fig,ax = plt.subplots(1)
heatmap = ax.pcolor(df, edgecolors='k')

cbar = plt.colorbar(heatmap, pad=0.1)

bx = ax.twinx()

ax.set_yticks(np.arange(0.5, len(df.index), 1))
ax.set_xticks(np.arange(0.5, len(df.columns), 1), )
ax.set_xticklabels(df.columns, rotation=10)
ax.set_yticklabels(df.index,fontsize=10)
bx.set_yticks(np.arange(0.5, len(df.index), 1))
bx.set_yticklabels(["V1","V2","V3"],fontsize=10)
ax.set_ylim([0,12])
bx.set_ylim([0,12])
ax.invert_yaxis()
bx.invert_yaxis()

plt.show()