如何使用matplotlib 0.99在两侧显示y轴?

时间:2013-07-18 03:21:03

标签: python matplotlib

我想在两边展示yaxis。在matplotlib 1.2中,我可以使用以下代码:

ax.tick_params(labelright = True)

但是,matplotlib 0.99中的Axes没有方法tick_params。有没有简单的方法在0.99做到这一点? TKS

修改 的 我得到了这个解决方案,随后是@Brian Cain的

ax2 = ax1.twinx()
ax2.set_yticks(ax1.get_yticks())
ax2.set_yticklabels([t.get_text() for t in ax1.get_yticklabels()])

1 个答案:

答案 0 :(得分:4)

这是example from matplotlib docs,每个Y轴上有不同的比例。如果您愿意,可以使用相同的比例。

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('time (s)')
# Make the y-axis label and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
for tl in ax1.get_yticklabels():
    tl.set_color('b')


ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r.')
ax2.set_ylabel('sin', color='r')
for tl in ax2.get_yticklabels():
    tl.set_color('r')
plt.show()

Resulting image