4

I'm using the following code to produce a sort of binary heatmap:

import numpy as np
import matplotlib.colors as mlc
import matplotlib.pyplot as mlp

states = ['AAAA', 'BBBB', 'CCCC', 'DDDD']
walk = [1, 2, 3, 2, 3, 2, 3, 2, 3, 2]

states_len = len(states)
walk_len = len(walk)

img = np.zeros((states_len, walk_len), dtype=float)

for i, s in enumerate(walk):
    img[s, i] = 1.0

figure, ax = mlp.subplots()

color_map = mlc.LinearSegmentedColormap.from_list('ColorMap', [(1.000, 1.000, 1.000), (0.984, 0.501, 0.447)])
ax.imshow(img, cmap=color_map, interpolation='none')

ax.set_xlabel('Steps', fontsize=13)
ax.set_xticks(np.arange(0, walk_len, 1))
ax.set_xticks(np.arange(-0.5, walk_len, 1), minor=True)
ax.set_xticklabels(np.arange(1, walk_len + 1, 1))

ax.set_ylabel('States', fontsize=13)
ax.set_yticks(np.arange(0, states_len, 1))
ax.set_yticks(np.arange(-.5, states_len, 1), minor=True)
ax.set_yticklabels(states)

ax.grid(which='minor', color='k')

ax.set_title('Walkplot (Sequence)', fontsize=15, fontweight='bold')

And it's producing weird results:

Screenshot

As you may notice, the color spots look not lined up with respect to the grid squares. On the top of that, sometimes grid lines don't have a uniform size, some looks bigger than others.

I think it must be related to the aspect ratio or to the figure size, but I'm clueless about a potential fix. How can I solve this issue?

My current setup:

  • Python 3.6 x64
  • PyCharm 2018.1
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • Any chance we can get a [mcve](https://stackoverflow.com/help/mcve) to play around with and try to find a solution? – Sheldore Jan 01 '19 at 19:19
  • Moreover, the grid lines not having a uniform size is just a visual thing I guess. If you simply zoon in your figure, you will see all grid lines have the same thickness. – Sheldore Jan 01 '19 at 19:22
  • @Bazingaa. They aren't lined up with the pixels. So either the pixels are a non-uniform size or the lines are not lined up correctly. Possibly both. Either way OP has stumbled onto something here. – Mad Physicist Jan 01 '19 at 19:25
  • @MadPhysicist: Yeah you are right. On my Mac, the vertical lines appear all same width but on my Desktop (non-retina) they appear unequal length – Sheldore Jan 01 '19 at 19:27
  • I fixed the code to make it verifiable. – Tommaso Belluzzo Jan 01 '19 at 19:28
  • @TommasoBelluzzo: Thanks. I ran it and I do not see any white spots in the output. I do see the different thickness of the lines but when I zoom in, they all become equal width. It shows that the thickness thing is partly a pixel issue – Sheldore Jan 01 '19 at 19:29
  • 2
    The lower the dpi, the more pronounced this problem. Hence using a dpi of 300 or above will let you get rid of it. It's still somehow a bug, however at least some matplotlib devs do argue that you should be using a higher dpi anyways. Also using a Cairo based backend (instead of Agg) may help. Nonetheless feel free to open an issue about it. – ImportanceOfBeingErnest Jan 01 '19 at 20:46
  • 1
    The champ is back. Happy new year Earnest :) – Sheldore Jan 01 '19 at 22:06

1 Answers1

3

As the comments indicated the problem seems to go away at higher DPI, for future users this is shown below,

50 DPI - Figsize 8, 6

100 DPI - Figsize 8. 6

However it is also influenced by the figsize parameter, set either with matplotlib rcParams or with figsize(height, width) in construction of the figure - larger figures require higher DPI for the display problem to disappear (though it may be necessary to view the full size images to see the effect, instead of the inline display), i.e.

50 DPI - Figsize 20,16

100 DPI - Figsize 20,16

300 DPI - Figsize 20,16

William Miller
  • 9,839
  • 3
  • 25
  • 46