There have been other questions about it but none seems to cover my exact question / none of their solutions "corrected" my problem.
import matplotlib.pyplot as plt
calendar = [[1,0,0,1,1,0,1],[0,0,1,1,1,0,1]]
fig, ax = plt.subplots()
namelist = ['alpha','beta']
yticks = [n for n in range(len(namelist))]
ax.set_yticks(yticks)
ax.set_yticklabels(namelist)
ax2 = ax.twinx()
ax2.set_yticks(ax.get_yticks())
ax2.set_yticklabels(['gamma','delta'])
im = ax.imshow(calendar, interpolation='none', cmap='summer')
plt.show()
Of course the upper is just a very simplified version but it should be enough to see the problem: When commenting out the two "ax2 lines" the plot is as I want but when adding the second y-axis the plot resizes like I don't want it. I want the alpha/beta to be at the same height as gamma/delta (such that no white space is remaining) but I'm failing for quite some time now. I've tried the get_yticks()
function (as proposed in some similar questions) but it didn't help.
Thanks in advance!
EDIT: One small detail: The solution should be "extendible" in the sense, that it should handle more y-values (alpha, beta, delta, ...) with their twin counterpart in front.
EDIT 2: (I wasn't sure if posting a new question was useful, so I just ask here:) I'm now trying to add a grid. I've again tried a lot but nothing seems to help, at best I can get a y-axis-grid. Check the following code (and try the same with those lines uncommented!):
import matplotlib.pyplot as plt
calendar = [[1,0,0,1,1,0,1],[0,0,1,1,1,0,1]]
fig, ax = plt.subplots()
namelist = ['alpha','beta']
yticks = [n for n in range(len(namelist))]
ax.set_xticks([num for num in range(len(calendar[0]))])
ax.set_xticks([num-0.5 for num in range(len(calendar[0]))],minor=True)
ax.set_yticks(yticks)
ax.set_yticks([num-0.5 for num in range(len(namelist))],minor=True)
ax.set_yticklabels(namelist)
im = ax.imshow(calendar, interpolation='none', cmap='summer')
ax.set_adjustable('box-forced')
ax.xaxis.grid(which='major',alpha=1,color='k',lw=2)
ax.xaxis.grid(which='minor',alpha=1,color='k',lw=2)
ax.yaxis.grid(which='minor',alpha=1,color='k',lw=2)
#ax2 = ax.twinx()
#ax2.set_yticks(ax.get_yticks())
#ax2.set_yticklabels(['gamma','delta'])
#im2 = ax2.imshow(calendar, interpolation='none', cmap='summer')
#ax2.set_adjustable('box-forced')
plt.show()
How can I get the grid to show up? Here a picture of how it is (without ax2). Basically shouldn't the code exactly give me the same but with the second label(s) on the right?!