Matplotlib spines/axes cut











up vote
1
down vote

favorite












Got this in my code with matplotlib library:



ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')


and it shows me :



I don't want this to happen!



I was expecting axes to cut! How to get cut axes?



Here's the full code:



import matplotlib.pyplot as plt

pgcd = lambda a, b: a if b==0 else pgcd(b, a%b)


def EKG(n):
ekg = [1, 2]
i = 2
while i < n:
j = 1
while j in ekg or pgcd(j, ekg[i-1]) == 1:
j += 1
ekg.append(j)
i += 1
return ekg


supEKG = lambda n: sum(a>n for a in EKG(n))

arr =
r = range(3, 100)
for n in r:
arr.append(supEKG(n))


f = plt.figure()
ax = f.add_subplot(1,1,1)
ax.plot(r, arr)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')

plt.show()









share|improve this question
























  • seaborn.despine(trim=True) will get you most of the there.
    – Paul H
    Nov 10 at 14:59










  • (i added the trim and offset functionality to despine and it's not as simple as one would think)
    – Paul H
    Nov 10 at 15:00










  • Thanks @paul-h I will give a try to seaborn later. Graphics on the homepage are effectively very impressive and code quite short.
    – david
    Nov 10 at 17:25















up vote
1
down vote

favorite












Got this in my code with matplotlib library:



ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')


and it shows me :



I don't want this to happen!



I was expecting axes to cut! How to get cut axes?



Here's the full code:



import matplotlib.pyplot as plt

pgcd = lambda a, b: a if b==0 else pgcd(b, a%b)


def EKG(n):
ekg = [1, 2]
i = 2
while i < n:
j = 1
while j in ekg or pgcd(j, ekg[i-1]) == 1:
j += 1
ekg.append(j)
i += 1
return ekg


supEKG = lambda n: sum(a>n for a in EKG(n))

arr =
r = range(3, 100)
for n in r:
arr.append(supEKG(n))


f = plt.figure()
ax = f.add_subplot(1,1,1)
ax.plot(r, arr)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')

plt.show()









share|improve this question
























  • seaborn.despine(trim=True) will get you most of the there.
    – Paul H
    Nov 10 at 14:59










  • (i added the trim and offset functionality to despine and it's not as simple as one would think)
    – Paul H
    Nov 10 at 15:00










  • Thanks @paul-h I will give a try to seaborn later. Graphics on the homepage are effectively very impressive and code quite short.
    – david
    Nov 10 at 17:25













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Got this in my code with matplotlib library:



ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')


and it shows me :



I don't want this to happen!



I was expecting axes to cut! How to get cut axes?



Here's the full code:



import matplotlib.pyplot as plt

pgcd = lambda a, b: a if b==0 else pgcd(b, a%b)


def EKG(n):
ekg = [1, 2]
i = 2
while i < n:
j = 1
while j in ekg or pgcd(j, ekg[i-1]) == 1:
j += 1
ekg.append(j)
i += 1
return ekg


supEKG = lambda n: sum(a>n for a in EKG(n))

arr =
r = range(3, 100)
for n in r:
arr.append(supEKG(n))


f = plt.figure()
ax = f.add_subplot(1,1,1)
ax.plot(r, arr)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')

plt.show()









share|improve this question















Got this in my code with matplotlib library:



ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')


and it shows me :



I don't want this to happen!



I was expecting axes to cut! How to get cut axes?



Here's the full code:



import matplotlib.pyplot as plt

pgcd = lambda a, b: a if b==0 else pgcd(b, a%b)


def EKG(n):
ekg = [1, 2]
i = 2
while i < n:
j = 1
while j in ekg or pgcd(j, ekg[i-1]) == 1:
j += 1
ekg.append(j)
i += 1
return ekg


supEKG = lambda n: sum(a>n for a in EKG(n))

arr =
r = range(3, 100)
for n in r:
arr.append(supEKG(n))


f = plt.figure()
ax = f.add_subplot(1,1,1)
ax.plot(r, arr)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')

plt.show()






python matplotlib axes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 17:14

























asked Nov 10 at 14:55









david

897




897












  • seaborn.despine(trim=True) will get you most of the there.
    – Paul H
    Nov 10 at 14:59










  • (i added the trim and offset functionality to despine and it's not as simple as one would think)
    – Paul H
    Nov 10 at 15:00










  • Thanks @paul-h I will give a try to seaborn later. Graphics on the homepage are effectively very impressive and code quite short.
    – david
    Nov 10 at 17:25


















  • seaborn.despine(trim=True) will get you most of the there.
    – Paul H
    Nov 10 at 14:59










  • (i added the trim and offset functionality to despine and it's not as simple as one would think)
    – Paul H
    Nov 10 at 15:00










  • Thanks @paul-h I will give a try to seaborn later. Graphics on the homepage are effectively very impressive and code quite short.
    – david
    Nov 10 at 17:25
















seaborn.despine(trim=True) will get you most of the there.
– Paul H
Nov 10 at 14:59




seaborn.despine(trim=True) will get you most of the there.
– Paul H
Nov 10 at 14:59












(i added the trim and offset functionality to despine and it's not as simple as one would think)
– Paul H
Nov 10 at 15:00




(i added the trim and offset functionality to despine and it's not as simple as one would think)
– Paul H
Nov 10 at 15:00












Thanks @paul-h I will give a try to seaborn later. Graphics on the homepage are effectively very impressive and code quite short.
– david
Nov 10 at 17:25




Thanks @paul-h I will give a try to seaborn later. Graphics on the homepage are effectively very impressive and code quite short.
– david
Nov 10 at 17:25












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










look at below sample, that will help you to get what you want.



"""
Broken axis example, where the x-axis will have a portion cut out.
"""
import matplotlib.pylab as plt
import numpy as np

x = np.linspace(0, 10, 100)
x[75:] = np.linspace(40, 42.5, 25)

y = np.sin(x)

f, (ax, ax2) = plt.subplots(1, 2, sharey=True, facecolor='w')

# plot the same data on both axes
ax.plot(x, y)
ax2.plot(x, y)

ax.set_xlim(0, 7.5)
ax2.set_xlim(40, 42.5)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright='off')
ax2.yaxis.tick_right()

# This looks pretty good, and was fairly painless, but you can get that
# cut-out diagonal lines look with just a bit more work. The important
# thing to know here is that in axes coordinates, which are always
# between 0-1, spine endpoints are at these locations (0,0), (0,1),
# (1,0), and (1,1). Thus, we just need to put the diagonals in the
# appropriate corners of each of our axes, and so long as we use the
# right transform and disable clipping.

d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((-d, +d), (-d, +d), **kwargs)

# What's cool about this is that now if we vary the distance between
# ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(),
# the diagonal lines will move accordingly, and stay right at the tips
# of the spines they are 'breaking'

plt.show()


this approach was in broken axes.






share|improve this answer





















  • OK, thanks @saeed-heidari. I got it working in looking to the code with set_xlim method. Finally I use : ax.set_ylim(-.1, max(arr) + .5)
    – david
    Nov 10 at 17:21











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240144%2fmatplotlib-spines-axes-cut%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








up vote
3
down vote



accepted










look at below sample, that will help you to get what you want.



"""
Broken axis example, where the x-axis will have a portion cut out.
"""
import matplotlib.pylab as plt
import numpy as np

x = np.linspace(0, 10, 100)
x[75:] = np.linspace(40, 42.5, 25)

y = np.sin(x)

f, (ax, ax2) = plt.subplots(1, 2, sharey=True, facecolor='w')

# plot the same data on both axes
ax.plot(x, y)
ax2.plot(x, y)

ax.set_xlim(0, 7.5)
ax2.set_xlim(40, 42.5)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright='off')
ax2.yaxis.tick_right()

# This looks pretty good, and was fairly painless, but you can get that
# cut-out diagonal lines look with just a bit more work. The important
# thing to know here is that in axes coordinates, which are always
# between 0-1, spine endpoints are at these locations (0,0), (0,1),
# (1,0), and (1,1). Thus, we just need to put the diagonals in the
# appropriate corners of each of our axes, and so long as we use the
# right transform and disable clipping.

d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((-d, +d), (-d, +d), **kwargs)

# What's cool about this is that now if we vary the distance between
# ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(),
# the diagonal lines will move accordingly, and stay right at the tips
# of the spines they are 'breaking'

plt.show()


this approach was in broken axes.






share|improve this answer





















  • OK, thanks @saeed-heidari. I got it working in looking to the code with set_xlim method. Finally I use : ax.set_ylim(-.1, max(arr) + .5)
    – david
    Nov 10 at 17:21















up vote
3
down vote



accepted










look at below sample, that will help you to get what you want.



"""
Broken axis example, where the x-axis will have a portion cut out.
"""
import matplotlib.pylab as plt
import numpy as np

x = np.linspace(0, 10, 100)
x[75:] = np.linspace(40, 42.5, 25)

y = np.sin(x)

f, (ax, ax2) = plt.subplots(1, 2, sharey=True, facecolor='w')

# plot the same data on both axes
ax.plot(x, y)
ax2.plot(x, y)

ax.set_xlim(0, 7.5)
ax2.set_xlim(40, 42.5)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright='off')
ax2.yaxis.tick_right()

# This looks pretty good, and was fairly painless, but you can get that
# cut-out diagonal lines look with just a bit more work. The important
# thing to know here is that in axes coordinates, which are always
# between 0-1, spine endpoints are at these locations (0,0), (0,1),
# (1,0), and (1,1). Thus, we just need to put the diagonals in the
# appropriate corners of each of our axes, and so long as we use the
# right transform and disable clipping.

d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((-d, +d), (-d, +d), **kwargs)

# What's cool about this is that now if we vary the distance between
# ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(),
# the diagonal lines will move accordingly, and stay right at the tips
# of the spines they are 'breaking'

plt.show()


this approach was in broken axes.






share|improve this answer





















  • OK, thanks @saeed-heidari. I got it working in looking to the code with set_xlim method. Finally I use : ax.set_ylim(-.1, max(arr) + .5)
    – david
    Nov 10 at 17:21













up vote
3
down vote



accepted







up vote
3
down vote



accepted






look at below sample, that will help you to get what you want.



"""
Broken axis example, where the x-axis will have a portion cut out.
"""
import matplotlib.pylab as plt
import numpy as np

x = np.linspace(0, 10, 100)
x[75:] = np.linspace(40, 42.5, 25)

y = np.sin(x)

f, (ax, ax2) = plt.subplots(1, 2, sharey=True, facecolor='w')

# plot the same data on both axes
ax.plot(x, y)
ax2.plot(x, y)

ax.set_xlim(0, 7.5)
ax2.set_xlim(40, 42.5)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright='off')
ax2.yaxis.tick_right()

# This looks pretty good, and was fairly painless, but you can get that
# cut-out diagonal lines look with just a bit more work. The important
# thing to know here is that in axes coordinates, which are always
# between 0-1, spine endpoints are at these locations (0,0), (0,1),
# (1,0), and (1,1). Thus, we just need to put the diagonals in the
# appropriate corners of each of our axes, and so long as we use the
# right transform and disable clipping.

d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((-d, +d), (-d, +d), **kwargs)

# What's cool about this is that now if we vary the distance between
# ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(),
# the diagonal lines will move accordingly, and stay right at the tips
# of the spines they are 'breaking'

plt.show()


this approach was in broken axes.






share|improve this answer












look at below sample, that will help you to get what you want.



"""
Broken axis example, where the x-axis will have a portion cut out.
"""
import matplotlib.pylab as plt
import numpy as np

x = np.linspace(0, 10, 100)
x[75:] = np.linspace(40, 42.5, 25)

y = np.sin(x)

f, (ax, ax2) = plt.subplots(1, 2, sharey=True, facecolor='w')

# plot the same data on both axes
ax.plot(x, y)
ax2.plot(x, y)

ax.set_xlim(0, 7.5)
ax2.set_xlim(40, 42.5)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright='off')
ax2.yaxis.tick_right()

# This looks pretty good, and was fairly painless, but you can get that
# cut-out diagonal lines look with just a bit more work. The important
# thing to know here is that in axes coordinates, which are always
# between 0-1, spine endpoints are at these locations (0,0), (0,1),
# (1,0), and (1,1). Thus, we just need to put the diagonals in the
# appropriate corners of each of our axes, and so long as we use the
# right transform and disable clipping.

d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((-d, +d), (-d, +d), **kwargs)

# What's cool about this is that now if we vary the distance between
# ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(),
# the diagonal lines will move accordingly, and stay right at the tips
# of the spines they are 'breaking'

plt.show()


this approach was in broken axes.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 15:56









saeed heidari

1644




1644












  • OK, thanks @saeed-heidari. I got it working in looking to the code with set_xlim method. Finally I use : ax.set_ylim(-.1, max(arr) + .5)
    – david
    Nov 10 at 17:21


















  • OK, thanks @saeed-heidari. I got it working in looking to the code with set_xlim method. Finally I use : ax.set_ylim(-.1, max(arr) + .5)
    – david
    Nov 10 at 17:21
















OK, thanks @saeed-heidari. I got it working in looking to the code with set_xlim method. Finally I use : ax.set_ylim(-.1, max(arr) + .5)
– david
Nov 10 at 17:21




OK, thanks @saeed-heidari. I got it working in looking to the code with set_xlim method. Finally I use : ax.set_ylim(-.1, max(arr) + .5)
– david
Nov 10 at 17:21


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240144%2fmatplotlib-spines-axes-cut%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."