import numpy as np import scipy.stats as ss import pandas as pd import matplotlib.pyplot as plt import seaborn as sns df=pd.read_excel("./tmp_apply.xlsx") sns.set_context(font_scale=20) plt.rcParams['font.family'] = 'FangSong' plv_tb=pd.pivot_table(df,values="sale",index=["FactoryName","JiJXH"],columns=["Xian"],aggfunc=np.mean) plt.figure(figsize=(200,10)) sns.heatmap(plv_tb,vmin=plv_tb.min().min(), vmax=plv_tb.max().max(),cmap=sns.color_palette("RdYlBu",n_colors=10),annot=True, fmt='g', annot_kws={"size": 20}) plt.show()设置热力图和色阶轴距离距离
时间: 2023-12-06 18:45:12 浏览: 141
可以使用`plt.subplots_adjust()`函数来调整热力图和色阶轴的距离,具体实现代码如下:
``` python
import numpy as np
import scipy.stats as ss
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df=pd.read_excel("./tmp_apply.xlsx")
sns.set_context(font_scale=20)
plt.rcParams['font.family'] = 'FangSong'
plv_tb=pd.pivot_table(df,values="sale",index=["FactoryName","JiJXH"],columns=["Xian"],aggfunc=np.mean)
fig, ax = plt.subplots(figsize=(200,10))
sns.heatmap(plv_tb,vmin=plv_tb.min().min(), vmax=plv_tb.max().max(),cmap=sns.color_palette("RdYlBu",n_colors=10),annot=True, fmt='g', annot_kws={"size": 20}, ax=ax)
plt.subplots_adjust(left=0.2, bottom=0.2) # 调整左边和底部的距离
plt.show()
```
通过`plt.subplots()`函数创建一个`fig`和`ax`对象,然后在`sns.heatmap()`函数中指定`ax=ax`参数,将绘图的轴设置为`ax`,最后调用`plt.subplots_adjust()`函数来调整热力图和色阶轴的距离。
阅读全文