1

I am trying to add a secondary y labels to a heatmap using twinx(). But the the primary y axis, ticks and labels are always offset after I set up the secondary y labels. See the results below. The second image shows the what the output was when the secondary axis was removed.

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# sphinx_gallery_thumbnail_number = 2

vegetables = ["cucumber", "tomato", "lettuce", "asparagus", "potato", "wheat", "barley"]
v2 = ["1", "2", "3", "4", "5", "6", "7"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
           "Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]

harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
                    [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
                    [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
                    [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
                    [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
                    [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
                    [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])


fig, ax1 = plt.subplots()
im = ax1.imshow(harvest)

ax1.set_xticks(np.arange(len(farmers)))
ax1.set_yticks(np.arange(len(vegetables)))
ax1.set_xticklabels(farmers)
ax1.set_yticklabels(vegetables)

ax2 = ax1.twinx()
ax2.set_yticks(np.arange(len(v2)))
ax2.set_yticklabels(v2)

plt.setp(ax1.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")

for i in range(len(vegetables)):
    for j in range(len(farmers)):
        text = ax1.text(j, i, harvest[i, j], ha="center", va="center", color="w")
        
fig.tight_layout()
plt.show()

With secondary axis and labels

Without secondary axis and labels

Stephen127
  • 305
  • 2
  • 3
  • 10
  • Perhaps, could you illustrate how the figure should look like? I am getting confused with the sentence "*the primary y axis, ticks and labels are always offset after I set up the secondary y labels*" – max Dec 02 '20 at 07:11

0 Answers0