Python Matplotlib - Misaligned Grid Lines and Color Fills
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:
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
python python-3.x matplotlib plot
|
show 3 more comments
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:
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
python python-3.x matplotlib plot
Any chance we can get a mcve to play around with and try to find a solution?
– Bazingaa
Jan 1 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.
– Bazingaa
Jan 1 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 1 at 19:25
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 1 at 20:46
1
The champ is back. Happy new year Earnest :)
– Bazingaa
Jan 1 at 22:06
|
show 3 more comments
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:
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
python python-3.x matplotlib plot
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:
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
python python-3.x matplotlib plot
python python-3.x matplotlib plot
edited Jan 15 at 17:39
Tommaso Belluzzo
asked Jan 1 at 19:04


Tommaso BelluzzoTommaso Belluzzo
17.7k65181
17.7k65181
Any chance we can get a mcve to play around with and try to find a solution?
– Bazingaa
Jan 1 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.
– Bazingaa
Jan 1 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 1 at 19:25
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 1 at 20:46
1
The champ is back. Happy new year Earnest :)
– Bazingaa
Jan 1 at 22:06
|
show 3 more comments
Any chance we can get a mcve to play around with and try to find a solution?
– Bazingaa
Jan 1 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.
– Bazingaa
Jan 1 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 1 at 19:25
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 1 at 20:46
1
The champ is back. Happy new year Earnest :)
– Bazingaa
Jan 1 at 22:06
Any chance we can get a mcve to play around with and try to find a solution?
– Bazingaa
Jan 1 at 19:19
Any chance we can get a mcve to play around with and try to find a solution?
– Bazingaa
Jan 1 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.
– Bazingaa
Jan 1 at 19:22
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.
– Bazingaa
Jan 1 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 1 at 19:25
@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 1 at 19:25
2
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 1 at 20:46
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 1 at 20:46
1
1
The champ is back. Happy new year Earnest :)
– Bazingaa
Jan 1 at 22:06
The champ is back. Happy new year Earnest :)
– Bazingaa
Jan 1 at 22:06
|
show 3 more comments
1 Answer
1
active
oldest
votes
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998166%2fpython-matplotlib-misaligned-grid-lines-and-color-fills%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Jan 2 at 0:34


William MillerWilliam Miller
1,407317
1,407317
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998166%2fpython-matplotlib-misaligned-grid-lines-and-color-fills%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Any chance we can get a mcve to play around with and try to find a solution?
– Bazingaa
Jan 1 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.
– Bazingaa
Jan 1 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 1 at 19:25
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 1 at 20:46
1
The champ is back. Happy new year Earnest :)
– Bazingaa
Jan 1 at 22:06