How can I draw axis lines inside a plot in Matplotlib?
When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2
and -2 < y < 2
, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.
python matplotlib
add a comment |
When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2
and -2 < y < 2
, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.
python matplotlib
When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.
– cel
Sep 16 '15 at 10:47
2
@cel: As someone who regularly deals with matplotlib, I think the question is quite clear.
– Hannes Ovrén
Sep 16 '15 at 10:50
add a comment |
When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2
and -2 < y < 2
, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.
python matplotlib
When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2
and -2 < y < 2
, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.
python matplotlib
python matplotlib
asked Sep 16 '15 at 10:32
Thomas ArildsenThomas Arildsen
339518
339518
When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.
– cel
Sep 16 '15 at 10:47
2
@cel: As someone who regularly deals with matplotlib, I think the question is quite clear.
– Hannes Ovrén
Sep 16 '15 at 10:50
add a comment |
When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.
– cel
Sep 16 '15 at 10:47
2
@cel: As someone who regularly deals with matplotlib, I think the question is quite clear.
– Hannes Ovrén
Sep 16 '15 at 10:50
When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.
– cel
Sep 16 '15 at 10:47
When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.
– cel
Sep 16 '15 at 10:47
2
2
@cel: As someone who regularly deals with matplotlib, I think the question is quite clear.
– Hannes Ovrén
Sep 16 '15 at 10:50
@cel: As someone who regularly deals with matplotlib, I think the question is quite clear.
– Hannes Ovrén
Sep 16 '15 at 10:50
add a comment |
2 Answers
2
active
oldest
votes
This is well documented in the spines example (old link) / spine placement demo (new link).
You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')
), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')
).
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)
ax = fig.add_subplot(111)
ax.set_title('zeroed spines')
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
# remove the ticks from the top and right edges
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
add a comment |
I can at least give a half-complete answer.
Yes, you can easily draw the axis lines. It is as simple as
plt.axvline(0)
plt.axhline(0)
The original axes will remain, but can be turned off with plt.axis('off')
.
It will also not give you any tick marks.
Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.
– Thomas Arildsen
Sep 16 '15 at 11:00
1
I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past thematplotlib.pyplot
module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.
– Hannes Ovrén
Sep 16 '15 at 11:03
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%2f32606155%2fhow-can-i-draw-axis-lines-inside-a-plot-in-matplotlib%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is well documented in the spines example (old link) / spine placement demo (new link).
You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')
), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')
).
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)
ax = fig.add_subplot(111)
ax.set_title('zeroed spines')
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
# remove the ticks from the top and right edges
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
add a comment |
This is well documented in the spines example (old link) / spine placement demo (new link).
You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')
), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')
).
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)
ax = fig.add_subplot(111)
ax.set_title('zeroed spines')
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
# remove the ticks from the top and right edges
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
add a comment |
This is well documented in the spines example (old link) / spine placement demo (new link).
You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')
), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')
).
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)
ax = fig.add_subplot(111)
ax.set_title('zeroed spines')
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
# remove the ticks from the top and right edges
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
This is well documented in the spines example (old link) / spine placement demo (new link).
You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')
), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')
).
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)
ax = fig.add_subplot(111)
ax.set_title('zeroed spines')
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
# remove the ticks from the top and right edges
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
edited Nov 15 '18 at 2:15
ImportanceOfBeingErnest
135k13151228
135k13151228
answered Sep 16 '15 at 11:02
tmdavisontmdavison
29k66880
29k66880
add a comment |
add a comment |
I can at least give a half-complete answer.
Yes, you can easily draw the axis lines. It is as simple as
plt.axvline(0)
plt.axhline(0)
The original axes will remain, but can be turned off with plt.axis('off')
.
It will also not give you any tick marks.
Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.
– Thomas Arildsen
Sep 16 '15 at 11:00
1
I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past thematplotlib.pyplot
module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.
– Hannes Ovrén
Sep 16 '15 at 11:03
add a comment |
I can at least give a half-complete answer.
Yes, you can easily draw the axis lines. It is as simple as
plt.axvline(0)
plt.axhline(0)
The original axes will remain, but can be turned off with plt.axis('off')
.
It will also not give you any tick marks.
Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.
– Thomas Arildsen
Sep 16 '15 at 11:00
1
I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past thematplotlib.pyplot
module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.
– Hannes Ovrén
Sep 16 '15 at 11:03
add a comment |
I can at least give a half-complete answer.
Yes, you can easily draw the axis lines. It is as simple as
plt.axvline(0)
plt.axhline(0)
The original axes will remain, but can be turned off with plt.axis('off')
.
It will also not give you any tick marks.
I can at least give a half-complete answer.
Yes, you can easily draw the axis lines. It is as simple as
plt.axvline(0)
plt.axhline(0)
The original axes will remain, but can be turned off with plt.axis('off')
.
It will also not give you any tick marks.
answered Sep 16 '15 at 10:48
Hannes OvrénHannes Ovrén
13.4k54863
13.4k54863
Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.
– Thomas Arildsen
Sep 16 '15 at 11:00
1
I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past thematplotlib.pyplot
module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.
– Hannes Ovrén
Sep 16 '15 at 11:03
add a comment |
Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.
– Thomas Arildsen
Sep 16 '15 at 11:00
1
I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past thematplotlib.pyplot
module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.
– Hannes Ovrén
Sep 16 '15 at 11:03
Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.
– Thomas Arildsen
Sep 16 '15 at 11:00
Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.
– Thomas Arildsen
Sep 16 '15 at 11:00
1
1
I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the
matplotlib.pyplot
module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.– Hannes Ovrén
Sep 16 '15 at 11:03
I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the
matplotlib.pyplot
module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.– Hannes Ovrén
Sep 16 '15 at 11:03
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%2f32606155%2fhow-can-i-draw-axis-lines-inside-a-plot-in-matplotlib%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
When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.
– cel
Sep 16 '15 at 10:47
2
@cel: As someone who regularly deals with matplotlib, I think the question is quite clear.
– Hannes Ovrén
Sep 16 '15 at 10:50