第1节 python画图01-双纵轴

1 导入需要的库

import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# 设置中文显示
font = {'family':'Microsoft YaHei', 'weight':'bold', 'size':12}
plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
plt.rcParams['axes.unicode_minus'] = False
matplotlib.rc('font', **font)

2 构造数据

x = np.arange(1,750,20)
y1 = np.sin(2*np.pi/365*x)*27
y2 = np.sin(2*np.pi/(365*2)*x)*500

3 画图显示数据

fig = plt.figure(figsize=(10, 4))
ax = fig.add_subplot(1,2,1)
ax.plot(x,y1,'r-',linewidth=2,label='y1-中文')
ax.legend()

ax = fig.add_subplot(1,2,2)
ax.bar(x,y2,width=10,color='b',label='y2-中文')
ax.legend()

4 双纵坐标显示数据

Last updated

Was this helpful?