matplotlib绘制双Y轴
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from scipy.interpolate import make_interp_spline #interpolate:篡改 spline:样条曲线

mpl.rcParams[“font.sans-serif”]=[“SimHei”]
mpl.rcParams[“axes.unicode_minus”]=False

fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8])

x = np.arange(0,9)
y1 = [5.4,5.8,6.0,5.9,5.8,5.7,5.6,5.4,5.3]

x_smooth = np.linspace(x.min(), x.max(), 300)
y1_smooth = make_interp_spline(x, y1)(x_smooth)
ln1, = plt.plot(x_smooth, y1_smooth,c="#0000FF")
ax1.scatter(x,y1,marker=“o”,c="#0000FF",s=60)
ax1.set_xlabel(“发酵时间(d)”)
ax1.set_ylabel(“pH值”)

ax2 = ax1.twinx()
y2 = [24.5,13.3,11.2,10.1,9.5,8.1,7.8,7.2,6.5]
y2_smooth = make_interp_spline(x, y2)(x_smooth)
ln2, = plt.plot(x_smooth,y2_smooth,c="#FF0000")
ax2.scatter(x,y2,marker=“s”,c="#FF0000",s=60)
ax2.set_ylabel(“残糖量(g/L)”)
plt.legend(handles=[ln1,ln2],labels=[“pH值”,“残糖量”],loc=“upper right”,edgecolor=“none”)
plt.show()

相关文章: