共享x轴,使用双y轴

1.代码如下: 

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

x=np.arange(1,100)

y=np.sin(x**2)

z=x*2

fig=plt.figure()

ax=fig.add_subplot(111)

ax.plot(x,y,label='sin',color='b')

ax.legend(loc=0)

ax.set_xlabel('Time')

ax.set_ylabel('sin')

ax2=ax.twinx()

ax2.plot(x,z,label='lines')

ax2.legend(loc=0)

ax2.set_ylabel('xxxxx')

plt.show()

结果显示:

共享x轴,使用双y轴 - nyang2016 - nyang的博客

 结论:twinx函数是关键,重中之重

猜你喜欢

转载自blog.csdn.net/ningyanggege/article/details/87931713
今日推荐