twiny() in matplotlib changes the y-axis scale

风格不统一 提交于 2019-12-21 23:27:12

问题


Plotting some data with matplotlib, it happens that inserting the ax.twiny() module changes the scale of the y axis. Before I use it:

And after:

I would like to avoid to insert manually the limits in the y-axis.

So why does this happen, and how to fix it?


回答1:


That's weird.

I've tried using the twiny() option and it does not change the scale:

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()

This gives without ax2 = ax1.twiny():

and with ax2 = ax1.twiny():

Is this also the way you implement the second x-axis?

Using ax2 you will be able to plot new data using the shared y-axis and the second x-axis.

Hope this helps!




回答2:


I have this issue too. Not sure what causes it. This is a more robust fix

ax2.set_xlim(ax.get_xlim())


来源:https://stackoverflow.com/questions/22853118/twiny-in-matplotlib-changes-the-y-axis-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!