我如何绘制一些数据,删除由该数据创建的轴,然后用不同比例的轴替换它们?
假设我有类似的东西:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([0,5])
plt.ylim([0,5])
plt.plot([0,1,2,3,4,5])
plt.show()
这会在 5x5 图中绘制一条线,两个轴上的范围都为 0 到 5。我想删除 0 到 5 轴并用 -25 到 25 轴替换它。这只会改变轴,但我不想移动任何数据,即它看起来与原始图相同,只是轴不同。我意识到这可以简单地通过移动数据来完成,但我不想改变数据。
你可以使用plt.xticks
找到标签的位置,然后设置标签到位置值的 5 倍。底层数据不变;只有标签。
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([0,5])
plt.ylim([0,5])
plt.plot([0,1,2,3,4,5])
locs, labels = plt.xticks()
labels = [float(item)*5 for item in locs]
plt.xticks(locs, labels)
plt.show()
yields
![enter image description here](https://i.stack.imgur.com/HjIsm.png)
或者,您可以更改股票代码格式化程序:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
N = 128
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(range(N+1))
plt.xlim([0,N])
plt.ylim([0,N])
ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: ('%g') % (x * 5.0)))
plt.show()
![enter image description here](https://i.stack.imgur.com/Lx3sT.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)