python画双y轴图像

很多时候可能需要在一个图中画出多条函数图像,但是可能y轴的物理含义不一样,或是数值范围相差较大,此时就需要双y轴。

matplotlib和seaborn都可以画双y轴图像。一个例子:

import seaborn as sns
import matplotlib.pyplot as plt

# ax1 for KDE, ax2 for CDF

f, ax1 = plt.subplots()
ax1.grid(True)
# ax1.set_ylim(0, 1)
ax1.set_ylabel('KDE')
ax1.set_xlabel('DATA')
ax1.set_title('KDE + CDF')
ax1.legend(loc=2)
sns.kdeplot(data, ax=ax1, lw=2, label='KDE') # KDE

ax2 = ax1.twinx() # the reason why it works
ax2.set_ylabel('CDF')
ax2.legend(loc=1)
ax2.hist(data, bins=50, cumulative=True, normed=True, histtype='step', color='red', lw=2, label='CDF') # CDF

plt.show()

猜你喜欢

转载自blog.csdn.net/jack_ricky/article/details/81939208
今日推荐