使用 seaborn 使热图的大小更大
Posted
技术标签:
【中文标题】使用 seaborn 使热图的大小更大【英文标题】:Make the size of a heatmap bigger with seaborn 【发布时间】:2016-12-19 05:33:49 【问题描述】:我用 seaborn 创建了一个热图
df1.index = pd.to_datetime(df1.index)
df1 = df1.set_index('TIMESTAMP')
df1 = df1.resample('30min').mean()
ax = sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5)
但问题是当数据框中有很多数据时,热图会太小,并且里面的值开始像附图中那样不清楚。
如何将热图的大小更改为更大? 谢谢
编辑
我试试:
df1.index = pd.to_datetime(df1.index)
fig, ax = plt.subplots(figsize=(10,10)) # Sample figsize in inches
sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5, ax=ax)
df1 = df1.set_index('TIMESTAMP')
df1 = df1.resample('1d').mean()
ax = sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5)
但我收到此错误:
KeyError Traceback (most recent call last)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py in get_loc(self, key, method, tolerance)
1944 try:
-> 1945 return self._engine.get_loc(key)
1946 except KeyError:
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)()
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)()
pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)()
pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)()
KeyError: 'TIMESTAMP'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-779-acaf05718dd8> in <module>()
2 fig, ax = plt.subplots(figsize=(10,10)) # Sample figsize in inches
3 sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5, ax=ax)
----> 4 df1 = df1.set_index('TIMESTAMP')
5 df1 = df1.resample('1d').mean()
6 ax = sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
2835 names.append(None)
2836 else:
-> 2837 level = frame[col]._values
2838 names.append(col)
2839 if drop:
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
1995 return self._getitem_multilevel(key)
1996 else:
-> 1997 return self._getitem_column(key)
1998
1999 def _getitem_column(self, key):
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
2002 # get column
2003 if self.columns.is_unique:
-> 2004 return self._get_item_cache(key)
2005
2006 # duplicate columns & possible reduce dimensionality
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
1348 res = cache.get(item)
1349 if res is None:
-> 1350 values = self._data.get(item)
1351 res = self._box_item_values(item, values)
1352 cache[item] = res
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
3288
3289 if not isnull(item):
-> 3290 loc = self.items.get_loc(item)
3291 else:
3292 indexer = np.arange(len(self.items))[isnull(self.items)]
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py in get_loc(self, key, method, tolerance)
1945 return self._engine.get_loc(key)
1946 except KeyError:
-> 1947 return self._engine.get_loc(self._maybe_cast_indexer(key))
1948
1949 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)()
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)()
pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)()
pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)()
KeyError: 'TIMESTAMP'
编辑
TypeError Traceback (most recent call last)
<ipython-input-890-86bff697504a> in <module>()
2 df2.resample('30min').mean()
3 fig, ax = plt.subplots()
----> 4 ax = sns.heatmap(df2.iloc[:, 1:6:], annot=True, linewidths=.5)
5 ax.set_yticklabels([i.strftime("%Y-%m-%d %H:%M:%S") for i in df2.index], rotation=0)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\seaborn\matrix.py in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, ax, xticklabels, yticklabels, mask, **kwargs)
483 plotter = _HeatMapper(data, vmin, vmax, cmap, center, robust, annot, fmt,
484 annot_kws, cbar, cbar_kws, xticklabels,
--> 485 yticklabels, mask)
486
487 # Add the pcolormesh kwargs here
C:\Users\Demonstrator\Anaconda3\lib\site-packages\seaborn\matrix.py in __init__(self, data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, cbar, cbar_kws, xticklabels, yticklabels, mask)
165 # Determine good default values for the colormapping
166 self._determine_cmap_params(plot_data, vmin, vmax,
--> 167 cmap, center, robust)
168
169 # Sort out the annotations
C:\Users\Demonstrator\Anaconda3\lib\site-packages\seaborn\matrix.py in _determine_cmap_params(self, plot_data, vmin, vmax, cmap, center, robust)
202 cmap, center, robust):
203 """Use some heuristics to set good defaults for colorbar and range."""
--> 204 calc_data = plot_data.data[~np.isnan(plot_data.data)]
205 if vmin is None:
206 vmin = np.percentile(calc_data, 2) if robust else calc_data.min()
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
【问题讨论】:
【参考方案1】:您可以通过传递tuple
来更改figsize
,以显示您希望保留的width, height
参数。
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10,10)) # Sample figsize in inches
sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5, ax=ax)
编辑
我记得回答过你的一个类似问题,你必须将索引设置为TIMESTAMP
。因此,您可以执行以下操作:
df = df.set_index('TIMESTAMP')
df.resample('30min').mean()
fig, ax = plt.subplots()
ax = sns.heatmap(df.iloc[:, 1:6:], annot=True, linewidths=.5)
ax.set_yticklabels([i.strftime("%Y-%m-%d %H:%M:%S") for i in df.index], rotation=0)
对于您发布的数据框的head
,该图如下所示:
【讨论】:
谢谢它的工作。请问最后一个问题;如何更改 TImesStamp 的日期格式。我添加了这一行: ax.set_yticklabels([i.strftime("%Y-%m-%d %H:%M:%S") for i in df1.TIMESTAMP], rotation=0) 但它不起作用! 它对我有用。你是按照我编辑的做的吗? @Nickil Maveli:看看我上次编辑的帖子?谢谢 哦...我明白了。当您执行print(df.dtypes)
时,您会得到什么?如果它们是objects
类型,则必须将它们转换为数字类型(int
\float
)。
我在使用的数据集上有分类列,例如性别,当我使用您提供的代码时,它会给我一个错误“ValueError: could not convert string to float: 'Female'”
【参考方案2】:
在 sns.heatmap 之前添加 plt.figure(figsize=(16,5))
并使用 figsize 数字直到获得所需的大小
...
plt.figure(figsize = (16,5))
ax = sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5)
【讨论】:
据我所知, plt.figure 应该适用于所有 seaborn 地块,对吧?当我为热图执行此操作时,它会为我的下一个图(配对图)尝试此操作,但这不起作用。你认为问题可能是什么?我在 jupyter notebook 中这样做 @deadcode :据我所知,plt.figure
仅适用于那些不接受 height
属性的 seaborn 地块。 pairplot
有一个 height
属性来设置图形的高度。在这种情况下,宽度将等于height
*aspect
。【参考方案3】:
我不知道如何使用代码来解决这个问题,但我确实手动调整了绘图右下角的控制面板,并调整了图形大小,如下所示:
f, ax = plt.subplots(figsize=(16, 12))
在此期间,直到您获得匹配大小的 colobar。这对我有用。
【讨论】:
以上是关于使用 seaborn 使热图的大小更大的主要内容,如果未能解决你的问题,请参考以下文章