如何设置使用的绘图的纵横比twinx
?
下面我举三个例子:
- without
twinx
突出显示我如何设置宽高比
- 仅与
twinx
说明如何重置先前定义的宽高比
- 我尝试定义两者的长宽比
y
使用时的轴twinx
(这不起作用)
我正在使用 matplotlib 1.3.0
以下脚本说明了如何设置简单绘图的纵横比:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,1.6,50) + 50.0
fig = plt.figure()
ax = fig.add_subplot(111)
XLIM = [50.0, 51.6]
YLIM = [0.0, 1.1, 0.0, 11.0]
ax.plot(x,np.sin(x-50.0),'b')
ax.set_xlim([XLIM[0], XLIM[1]])
ax.set_ylim([YLIM[0], YLIM[1]])
ax.set_xticks(np.arange(XLIM[0], XLIM[1], 0.2))
ax.set_yticks(np.arange(YLIM[0], YLIM[1]+0.1, 0.1)[:-1])
ax.grid(True,which='major',linestyle='solid')
ax.set_aspect((XLIM[1]-XLIM[0])/(YLIM[1]-YLIM[0]))
plt.show()
生成的图形具有定义的纵横比:
![first plot](https://i.stack.imgur.com/mI2cd.png)
但是,如果我想添加一个额外的y
- 轴使用twinx()
函数,宽高比被重置:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,1.6,50) + 50.0
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
XLIM = [50.0, 51.6]
YLIM = [0.0, 1.1, 0.0, 11.0]
ax.plot(x,np.sin(x-50.0),'b')
ax.set_xlim([XLIM[0], XLIM[1]])
ax.set_ylim([YLIM[0], YLIM[1]])
ax.set_xticks(np.arange(XLIM[0], XLIM[1], 0.2))
ax.set_yticks(np.arange(YLIM[0], YLIM[1]+0.1, 0.1)[:-1])
ax.grid(True,which='major',linestyle='solid')
ax.set_aspect((XLIM[1]-XLIM[0])/(YLIM[1]-YLIM[0]))
plt.show()
得到这个图(其中先前定义的y
-重置左侧的限制):
![first twinx attempt](https://i.stack.imgur.com/jKs0g.png)
现在,尝试再次指定纵横比不会产生预期的结果:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,1.6,50) + 50.0
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
XLIM = [50.0, 51.6]
YLIM = [0.0, 1.1, 0.0, 11.0]
ax.plot(x,np.sin(x-50.0),'b')
ax2.plot(x,np.cos(x-50.0)*10.,'r')
ax.set_xlim([XLIM[0], XLIM[1]])
ax.set_ylim([YLIM[0], YLIM[1]])
ax2.set_ylim([YLIM[2], YLIM[3]])
ax.set_xticks(np.arange(XLIM[0], XLIM[1], 0.2))
ax.set_yticks(np.arange(YLIM[0], YLIM[1]+0.1, 0.1)[:-1])
ax2.set_yticks(np.arange(YLIM[2], YLIM[3]+1.0, 1.0))
ax.grid(True,which='major',linestyle='solid')
ax.set_aspect((XLIM[1]-XLIM[0])/(YLIM[1]-YLIM[0]))
ax2.set_aspect((XLIM[1]-XLIM[0])/(YLIM[3]-YLIM[2]))
plt.show()
这会产生:
![attempt to fix the aspect ratio](https://i.stack.imgur.com/M3NxQ.png)
为了生成像第一个这样的情节,但有两个情节,我缺少什么y
- 不同尺度的轴?使用时如何定义纵横比twinx
?