【双y轴图】python制作双y轴图

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 1, num=10)
y1 = x ** 2
y2 = -x ** 2
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1, 'r', label="y1")
ax1.set_ylabel('y1')
ax2 = ax1.twinx()  # this is the important function
ax2.plot(x, y2, 'g', label="y2")
fig.legend()  # 合并图例
ax2.set_ylabel('y2')
ax2.set_xlabel('x')
plt.show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43486780/article/details/112406637
今日推荐