在不更改数据的情况下更改轴(Python)

2024-09-28 20:53:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我如何绘制一些数据,删除由这些数据创建的轴,并用不同比例的轴替换它们?在

假设我有这样的东西:

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轴替换它。这只会改变坐标轴,但我不想移动任何数据,也就是说,它看起来与原始绘图相同,只是轴不同而已。我意识到这可以简单地通过移动数据来实现,但我不想更改数据。在


Tags: 数据importadd绘图matplotlibasfig绘制
1条回答
网友
1楼 · 发布于 2024-09-28 20:53:10

您可以使用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()

收益率

enter image description here


或者,您可以更改ticker formatter:

^{pr2}$

enter image description here

相关问题 更多 >