如何将刻度标签从左脊椎移开 [英] How to move tick labels off left spine
本文介绍了如何将刻度标签从左脊椎移开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 matplotlib 中,当我移动左脊椎时,如何将 y 刻度标签移动到图表区域的最左侧?
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?
代码:
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')
输出:
但是,我想得到这个:
推荐答案
将y-ticklabel的变换更改回原始的y轴变换将为您在轴的左侧提供所需的刻度.
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())
完整的代码以提高可重复性
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()
这篇关于如何将刻度标签从左脊椎移开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文