python matplotlib实现双Y轴的实例

yipeiwu_com5年前Python基础

如下所示:

import matplotlib.pyplot as plt
import numpy as np
 
x = np.arange(0., np.e, 0.01)
y1 = np.exp(-x)
y2 = np.log(x)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1,'r',label="right");
ax1.legend(loc=1)
ax1.set_ylabel('Y values for exp(-x)');
ax2 = ax1.twinx() # this is the important function
ax2.plot(x, y2, 'g',label = "left")
ax2.legend(loc=2)
ax2.set_xlim([0, np.e]);
ax2.set_ylabel('Y values for ln(x)');
ax2.set_xlabel('Same X for both exp(-x) and ln(x)');
plt.show()

结果为:

python matplotlib实现双Y轴

以上这篇python matplotlib实现双Y轴的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

利用python在excel里面直接使用sql函数的方法

利用python在excel里面直接使用sql函数的方法

我们一般在Excel里面是使用数据连接属性里面写sql语句,或者vba里面利用ado组件执行sql语句。 新版的Excel里面带上了Power query的功能也可以使用Odbc.Dat...

python之super的使用小结

为什么需要super 在python没有引入super之前, 如果需要在子类中引用父类的方法, 一般写法如下: class Father: def whoami(self):...

详解Matplotlib绘图之属性设置

详解Matplotlib绘图之属性设置

关于Python数据分析在数学建模中的更多相关应用:Python数据分析在数学建模中的应用汇总(持续更新中!) (1)、导入库 import matplotlib.pyplot a...

tensorflow没有output结点,存储成pb文件的例子

tensorflow没有output结点,存储成pb文件的例子

Tensorflow中保存成pb file 需要 使用函数 graph_util.convert_variables_to_constants(sess, sess.graph_def,...

Python中的包和模块实例

一、实例和结果 1)实例的结构和具体的文件: 复制代码 代码如下: PyPackage │  PyCommonM.py │  __init__.py │ ├─p1Pa...