How to set aspect ratio for a graph using twinx?
The following are three examples:
- without
twinxto highlight how i set the aspect ratio - just
twinxto illustrate how previously defined aspect ratio reset - my attempt to define aspect ratio as for
y-axis when used twinx(which does not work)
I am using matplotlib 1.3.0
The following script illustrates how I set the aspect ratio of a simple graph:
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()
The picture shown has a certain aspect ratio:

However, if I want to add an extra y-axis using the function twinx(), the aspect ratio is reset:
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 - reset):

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

, , , y - ? twinx?