python画双y轴图像
很多时候可能需要在一个图中画出多条函数图像,但是可能y轴的物理含义不一样,或是数值范围相差较大,此时就需要双y轴。
matplotlib和seaborn都可以画双y轴图像。一个例子:
import seaborn as snsimport matplotlib.pyplot as plt# ax1 for KDE, ax2 for CDFf, 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') # KDEax2 = ax1.twinx() # the reason why it worksax2.set_ylabel('CDF')ax2.legend(loc=1)ax2.hist(data, bins=50, cumulative=True, normed=True, histtype='step', color='red', lw=2, label='CDF') # CDFplt.show()
赞 (0)
