绘制它两张轴如果你通常要绘制两列不同的轴,那么你也可以做以下情况:
import matplotlib as mpl
rc_fonts = {"text.usetex": True, "font.size": 30, 'mathtext.default': 'regular', 'axes.titlesize': 33, "axes.labelsize": 33, "legend.fontsize": 30, "xtick.labelsize": 30, "ytick.labelsize": 30, 'figure.titlesize': 33, 'figure.figsize': (15, 9.3), 'text.latex.preamble': [
r'\usepackage{amsmath,amssymb,bm,physics,lmodern}'], "font.family": "serif", "font.serif": "computer modern roman", }
mpl.rcParams.update(rc_fonts)
import matplotlib.pylab as plt
import matplotlib.dates as mdates
import pandas as pd
df = pd.DataFrame(zip(range(10), range(10)[::-1], [pd.datetime(year=2017, month=1, day=i) for i in range(1,11)]), columns=['y1', 'y2', 'date'])
plt.clf()
fig = plt.gcf()
ax1 = fig.add_subplot(111)
ax1.plot(df['date'], df['y1'], 'ko:', label='$y_1$')
ax1.set_xlabel('Date', labelpad=5)
ax1.set_ylabel('$y_1$')
ax1.set_title('Two $y$-axes', y=1.02)
ax1.legend(loc=(0.1, 0.5), handlelength=3, handletextpad=0.1, frameon=False, numpoints=1)
ax2 = ax1.twinx()
ax2.plot(df['date'], df['y2'], 'r^--', label='$y_2$')
ax2.set_ylabel('$y_2$')
ax2.legend(loc=(0.1, 0.4), handlelength=3, handletextpad=0.1, frameon=False, numpoints=1)
plt.gca().xaxis.set_minor_locator(mdates.DayLocator())
plt.gca().xaxis.set_minor_formatter(mdates.DateFormatter('%a\n%d-%m'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter(''))
ax1.xaxis.set_tick_params(which='both', pad=20)
plt.savefig('example.pdf', format='pdf', bbox_inches='tight')
其产生: