How do I let my matplotlib plot go beyond the axes?
Asked Answered
C

3

33

I have to translate an image plotting script from matlab to matplotlib/pylab, and I'm trying to achieve the same effect as the matlab image below:

image generated using matlab

As you can see, the z order of the plots seem to be higher than the z order of the grid, so the markers are not hidden by the axes. However, I can't figure out a way to do the same with my matplotlib image:

eimage generated with matplotlib

I'm wondering if it is possible to get the same display without having to increase the limits of the y axis.

Coparcenary answered 28/3, 2012 at 16:53 Comment(1)
I am sure you have good reasons to prefer the matlab behavior but IMHO drawing outside the axes is more of a bug than a feature.Staphylococcus
M
63

To get the marker to show beyond the axes you can turn the clipping off. This can be done using the keyword argument in the plot command clip_on=False.

For example:

import matplotlib.pyplot as plt
plt.plot(range(5), range(5), 'ro', markersize=20, clip_on=False, zorder=100)
plt.show()

enter image description here

Marine answered 28/3, 2012 at 19:10 Comment(2)
+1 Yes! I am fairly certain this is what OP was looking for, but that wasn't so clear -- to me at least -- until his comment on my (substandard) answer. Hopefully OP will accept this one. Thanks for posting.Catholicism
Great! Just what I needed.Hyperdulia
C
1

This is a complete example of how to use the zorder kwarg: http://matplotlib.sourceforge.net/examples/pylab_examples/zorder_demo.html
Note that a higher z-order equates to a graph-element being more in the foreground.

For your second question, have a look at the figsize kwarg to instances of the Figure class: http://matplotlib.sourceforge.net/api/figure_api.html?highlight=figsize#matplotlib.figure.Figure

If you run into issues, please post some of your code and we'll be able to give more-detailed recommendations. Best of luck.

Catholicism answered 28/3, 2012 at 17:3 Comment(2)
Thank you, I'll take a look at the 2 links you wrote down. For the code, I think that it is a default behaviour of matplotlib to consider that if the point itself (here for example 0.2;0 ) is visible, then it isn't an issue if the marker around it is not fully visible. That is the behaviour that I'm trying to changeCoparcenary
@nay: it looks like tom10 has the answer to your question. Happy coding!Catholicism
B
0

If you're plotting the lines one after the other, just change the order of the plotting calls and that would fix the z order.

Biannual answered 28/3, 2012 at 17:13 Comment(1)
Thanks for your answer, actually what I'm trying to do is to make the x order of the plots higher than the z order of the grid itself, so that the markers (for instance at (0.2;0) ) become fully visible like the matlab image.Coparcenary

© 2022 - 2024 — McMap. All rights reserved.