matplotlib中的twiny()改变了y轴刻度

时间:2014-04-04 03:44:53

标签: python matplotlib axis-labels

使用matplotlib绘制一些数据,插入ax.twiny()模块会改变y轴的比例。 在我使用之前: enter image description here

之后: enter image description here

我想避免在y轴上手动插入限制。

那为什么会发生这种情况,以及如何解决它?

2 个答案:

答案 0 :(得分:1)

这很奇怪。

我尝试过使用twiny()选项,它不会改变比例:

import numpy
import matplotlib.pylab as plt

x = numpy.linspace(0, 4. * numpy.pi, 1000)
y = numpy.cos(x)

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.plot(x, y)
ax2 = ax1.twiny()

这不包含ax2 = ax1.twiny()

enter image description here

ax2 = ax1.twiny()

enter image description here

这也是你实现第二个x轴的方式吗?

使用ax2,您将能够使用共享的y轴和第二个x轴绘制新数据。

希望这有帮助!

答案 1 :(得分:0)

我也有这个问题。不知道是什么原因造成的。这是一个更强大的修复程序

ax2.set_xlim(ax.get_xlim())