Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

python - How to move tick labels off left spine

In matplotlib, how do I move the y tick labels to the far left side of the charting areas when I have moved the left spine?

Code:

import matplotlib.pyplot as plt

X = range(-10,5)
y = [i**2 for i in X]

fig, ax = plt.subplots(1,1, figsize=(10,8))
plt.plot(X, y)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_position('zero')

Outputs:

enter image description here

However, I would like to get this:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Changing the transformation of the y-ticklabels back to the original y-axis transform would give you the desired ticks on the left side of the axes.

plt.setp(ax.get_yticklabels(), transform=ax.get_yaxis_transform())

enter image description here

Complete code for reproducibility

import matplotlib.pyplot as plt

X = range(-10,5)
y = [i**2 for i in X]

fig, ax = plt.subplots(1,1, figsize=(10,8))
plt.plot(X, y)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_position('zero')

plt.setp(ax.get_yticklabels(), transform=ax.get_yaxis_transform())

plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...