网络编程
位置:首页>> 网络编程>> Python编程>> python画双y轴图像的示例代码

python画双y轴图像的示例代码

作者:jack_ricky  发布时间:2021-12-07 16:07:12 

标签: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()

来源:https://blog.csdn.net/jack_ricky/article/details/81939208

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com