'How can I force seaborn or matplotlib to always render isolated values in heatmaps?
I am analyzing long series of events using heatmaps. Values of the column are most of the time 0 but occasionally are 1 unfortunately the rendering behaviour often hide the 1 occurrences because of the 0 surrounding them. I have tried to use antialiased=False but it did not solve the problem:
This code reproduce the issue:
import numpy as np
import pandas as pd
import seaborn as sns
d = pd.DataFrame(np.zeros((2000, 4)))
for i in range(4):
for j in [34,223,56,666]:
d[i][j] = 1
axS = sns.heatmap(d,antialiased=False)
There should be 4 lines instead only one is visible. Of course, if I stretch the plot I have better results but still some values are hidden.
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (10,30)
axB = sns.heatmap(d,antialiased=False)
I would like to force the rendering of isolated values. Is there any way to get this behaviour?
P.S. I need heatmaps because I compare multiple variables with float values, so spy for instance is not a good option for me.
Solution 1:[1]
If these events are infrequent, you could reduce the resolution of the vertical axis by aggregating the values into bins.
import numpy as np
import pandas as pd
import seaborn as sns
d = pd.DataFrame(np.zeros((2000, 4)))
for i in range(4):
for j in [34,223,56,666]:
d[i][j] = 1
x1 = d.index.min() - 1e-9
x2 = d.index.max()
bin_width = 50
bin_edge = np.arange(x1, x2 + bin_width, bin_width)
bin_center = np.arange(x1 + bin_width/2, x2, bin_width)
index_binned = pd.cut(d.index, bins=bin_edge, labels=bin_center)
d = d.join(pd.Series(index_binned, name="index_binned"))
d_binned = d.groupby('index_binned').sum()
sns.heatmap(data=d_binned, antialiased=False)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fokkerplanck |


