[英]Rename pandas dataframe columns when plotting heatmap with seaborn
I have a dataframe named recast
with the following structure: 我有一个名为
recast
的数据recast
,其结构如下:
data = [[ 374000,np.nan,749500,np.nan],
[np.nan,np.nan,298000,np.nan],
[540065.326633,np.nan,904750,np.nan],
[np.nan,np.nan,514000,np.nan],
[411000,np.nan,np.nan,np.nan]]
cols = pd.MultiIndex.from_tuples([('EUR, Oil (bbls)',c) for c in ['DE WITT','FAYETTE','GONZALES','LAVACA']], names=('','County'))
index = ['1776 ENERGY','ALMS','BURLINGTON','BXP','CHESAPEAKE']
recast=pd.DataFrame(data,index=index,columns=cols)
recast.index.name = 'ShortName'
print(recast)
EUR, Oil (bbls)
County DE WITT FAYETTE GONZALES LAVACA
ShortName
1776 ENERGY 374000.000000 NaN 749500.0 NaN
ALMS NaN NaN 298000.0 NaN
BURLINGTON 540065.326633 NaN 904750.0 NaN
BXP NaN NaN 514000.0 NaN
CHESAPEAKE 411000.000000 NaN NaN NaN
What I would like to do is make a heatmap that has only the County names and not the column names that it is giving me. 我想做的是制作一个仅包含县名而不提供给我的列名的热图。 When I call
.info()
on the dataframe I get: 当我在数据帧上调用
.info()
时,我得到:
<class 'pandas.core.frame.DataFrame'>
Index: 29 entries, 1776 ENERGY to VERDUN OIL
Data columns (total 4 columns):
(EUR, Oil (bbls), DE WITT) 14 non-null float64
(EUR, Oil (bbls), FAYETTE) 3 non-null float64
(EUR, Oil (bbls), GONZALES) 23 non-null float64
(EUR, Oil (bbls), LAVACA) 5 non-null float64
dtypes: float64(4)
So by default my x labels on the heatmap are, for example: EUR, Oil (bbls), DE WITT
, when I only want: DE WITT
. 因此,默认情况下,我在热图上的x标签例如是:
EUR, Oil (bbls), DE WITT
,而我只需要: DE WITT
。
I have tried elminating the unwanted part by trying rename the columns in the dataframe and by trying to set x_label, but can't get it to work. 我尝试通过尝试重命名数据帧中的列并尝试设置x_label来消除不需要的部分,但无法使其正常工作。 For plotting I am using the following code:
为了进行绘图,我使用以下代码:
fig = plt.figure(71)
fig = sns.heatmap(recast, cmap='coolwarm', linewidths=0.25, linecolor='black')
fig = plt.xticks(rotation=0)
plt.tight_layout
plt.show()
Also, I would like to remove the X and Y titles(?) that appear beneath/left of the labels. 另外,我想删除出现在标签下方/左侧的X和Y标题(?)。
You can use .xs()
to achieve your goal ( see documentation ). 您可以使用
.xs()
实现您的目标( 请参阅文档 )。
With the following sample: 带有以下示例:
data = [[ 374000,np.nan,749500,np.nan],
[np.nan,np.nan,298000,np.nan],
[540065.326633,np.nan,904750,np.nan],
[np.nan,np.nan,514000,np.nan],
[411000,np.nan,np.nan,np.nan]]
cols = pd.MultiIndex.from_tuples([('EUR, Oil (bbls)',c) for c in ['DE WITT','FAYETTE','GONZALES','LAVACA']], names=('','County'))
index = ['1776 ENERGY','ALMS','BURLINGTON','BXP','CHESAPEAKE']
recast=pd.DataFrame(data,index=index,columns=cols)
recast.index.name = 'ShortName'
print(recast)
EUR, Oil (bbls)
County DE WITT FAYETTE GONZALES LAVACA
ShortName
1776 ENERGY 374000.000000 NaN 749500.0 NaN
ALMS NaN NaN 298000.0 NaN
BURLINGTON 540065.326633 NaN 904750.0 NaN
BXP NaN NaN 514000.0 NaN
CHESAPEAKE 411000.000000 NaN NaN NaN
print(recast.xs('EUR, Oil (bbls)',axis=1, drop_level=True))
#axis = 1 for column multi-index selection
#drop_level = True to drop 'EUR Oil (bbls)' column level
County DE WITT FAYETTE GONZALES LAVACA
ShortName
1776 ENERGY 374000.000000 NaN 749500.0 NaN
ALMS NaN NaN 298000.0 NaN
BURLINGTON 540065.326633 NaN 904750.0 NaN
BXP NaN NaN 514000.0 NaN
CHESAPEAKE 411000.000000 NaN NaN NaN
To plot what you want I rewrite your plotting section with a minimal set: 要绘制您想要的内容,我用最少的一组重写您的绘图部分:
_recast = recast.xs('EUR, Oil (bbls)',axis=1, drop_level=True)
fig = sns.heatmap(_recast, cmap='coolwarm', linewidths=0.25, linecolor='black')
fig.set_xlabel(' ')
fig.set_ylabel(' ')
plt.show()
Note that to remove X and Y labels you can simply rename them with fig.set_[x or y]label()
functions: 请注意,要删除X和Y标签,您只需使用
fig.set_[x or y]label()
函数将它们重命名即可:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.