Hello. Using twinx of matplotlib, I tried to display grit on a plot with double left and right vertical axes, and not to make it unsightly.
Normally, the positions of these ticks and grids are inconsistent and unsightly. This time, I couldn't think of a good method, so I manually wrote set_yticks to make it consistent (matplotlib 2.0.2).
twinx_grid.py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
t = np.arange(0.0, 10.01, 0.01)
_, ax1 = plt.subplots()
ax2 = ax1.twinx()
title = 'sin'
s1 = np.sin(np.pi*t)
ax1.plot(t, s1, 'b.')
ax1.set_ylabel(title, color='b', fontsize=18)
title = 'linear'
s2 = t * 1.6
ax2.plot(t, s2, 'g-')
ax2.set_ylabel(title, color='g', fontsize=18)
#The number of ticks on the left and right vertical axes is equalized so that they are in the same position.
ax1.set_yticks(np.linspace(-1.0, 1.0, 5))
ax2.set_yticks(np.linspace(0, 20, 5))
ax1.set_ylim(-1.06, 1.06)
ax2.set_ylim(-0.6, 20.6)
ax1.tick_params(labelsize=18)
ax2.tick_params(labelsize=18)
ax1.set_xlabel('time (s)', fontsize=18)
ax1.grid(True, which='both')
plt.xlim(-0.2, 10.2)
plt.subplots_adjust(left=0.18, right=0.86, bottom=0.14, top=0.94)
plt.show()
Recommended Posts