Python Matplotlib - Spine coloring issue in a double-y axis plot
up vote
1
down vote
favorite
I'm writing a script to draw plot via matplotlib, the following code is an analogy to my original script that reproduce the issue that I've encountered.
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
fig = plt.figure()
data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]
ax = fig.add_subplot(111)
func(ax, data_1, 'r', 'left')
ax_1 = ax.twinx()
func(ax_1, data_2, 'b', 'right')
plt.show()
The expected plot should have both y axis colored, however, only right spine is colored, as shown below.

When zooming into left spine you can find a red shadow around y-axis, that means my colored spine is covered by another one, how to solve this issue by only modifying func()?
python matplotlib
add a comment |
up vote
1
down vote
favorite
I'm writing a script to draw plot via matplotlib, the following code is an analogy to my original script that reproduce the issue that I've encountered.
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
fig = plt.figure()
data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]
ax = fig.add_subplot(111)
func(ax, data_1, 'r', 'left')
ax_1 = ax.twinx()
func(ax_1, data_2, 'b', 'right')
plt.show()
The expected plot should have both y axis colored, however, only right spine is colored, as shown below.

When zooming into left spine you can find a red shadow around y-axis, that means my colored spine is covered by another one, how to solve this issue by only modifying func()?
python matplotlib
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm writing a script to draw plot via matplotlib, the following code is an analogy to my original script that reproduce the issue that I've encountered.
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
fig = plt.figure()
data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]
ax = fig.add_subplot(111)
func(ax, data_1, 'r', 'left')
ax_1 = ax.twinx()
func(ax_1, data_2, 'b', 'right')
plt.show()
The expected plot should have both y axis colored, however, only right spine is colored, as shown below.

When zooming into left spine you can find a red shadow around y-axis, that means my colored spine is covered by another one, how to solve this issue by only modifying func()?
python matplotlib
I'm writing a script to draw plot via matplotlib, the following code is an analogy to my original script that reproduce the issue that I've encountered.
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
fig = plt.figure()
data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]
ax = fig.add_subplot(111)
func(ax, data_1, 'r', 'left')
ax_1 = ax.twinx()
func(ax_1, data_2, 'b', 'right')
plt.show()
The expected plot should have both y axis colored, however, only right spine is colored, as shown below.

When zooming into left spine you can find a red shadow around y-axis, that means my colored spine is covered by another one, how to solve this issue by only modifying func()?
python matplotlib
python matplotlib
asked 15 hours ago
nochenon
336
336
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.
So:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
for pos in ['left', 'right']:
if pos != position:
ax.spines[pos].set_visible(False)
add a comment |
up vote
1
down vote
Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
if position == 'left':
other = 'right'
elif position == 'right':
other = 'left'
ax.spines[other].set_color('None')
Result:

This can solve my problem, but I'm still open for other beautiful solutions.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.
So:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
for pos in ['left', 'right']:
if pos != position:
ax.spines[pos].set_visible(False)
add a comment |
up vote
1
down vote
accepted
twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.
So:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
for pos in ['left', 'right']:
if pos != position:
ax.spines[pos].set_visible(False)
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.
So:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
for pos in ['left', 'right']:
if pos != position:
ax.spines[pos].set_visible(False)
twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.
So:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
for pos in ['left', 'right']:
if pos != position:
ax.spines[pos].set_visible(False)
answered 11 hours ago
gehbiszumeis
782118
782118
add a comment |
add a comment |
up vote
1
down vote
Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
if position == 'left':
other = 'right'
elif position == 'right':
other = 'left'
ax.spines[other].set_color('None')
Result:

This can solve my problem, but I'm still open for other beautiful solutions.
add a comment |
up vote
1
down vote
Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
if position == 'left':
other = 'right'
elif position == 'right':
other = 'left'
ax.spines[other].set_color('None')
Result:

This can solve my problem, but I'm still open for other beautiful solutions.
add a comment |
up vote
1
down vote
up vote
1
down vote
Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
if position == 'left':
other = 'right'
elif position == 'right':
other = 'left'
ax.spines[other].set_color('None')
Result:

This can solve my problem, but I'm still open for other beautiful solutions.
Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:
def func(ax, data, color, position): # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)
if position == 'left':
other = 'right'
elif position == 'right':
other = 'left'
ax.spines[other].set_color('None')
Result:

This can solve my problem, but I'm still open for other beautiful solutions.
answered 12 hours ago
nochenon
336
336
add a comment |
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183966%2fpython-matplotlib-spine-coloring-issue-in-a-double-y-axis-plot%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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