Nils
Nils

Reputation: 409

How to add a second scale to a heatmap?

This way I created my heatmap:

fig = plt.figure(figsize = (8.27, 13.69), dpi = 100)

fig, ax = plt.subplots(1,1,figsize = (8.27, 13.69))
fig = plt.gcf()

heatplot = ax.imshow(c, cmap='rainbow', aspect='auto',\
                         norm=MidpointNormalize(midpoint=0.90))

c is the array with the heatmap values, normalised to one.

With ax.set_xticklabels(), ax.set_xticks(), ax.set_yticklabels() and ax.set_yticks(), I've added the axis to its heatmap. Now I would like to add a second y-axis. The orientation of the colorbar is horizontal.

EDIT:

With the Help of Bazingaa i´ve got a second y-axis, but there is an offset:

enter image description here

ax1.set_yticks(np.arange(len(TITLES)), minor=False) centers the ticks.

Upvotes: 2

Views: 1263

Answers (2)

Nils
Nils

Reputation: 409

Solved the EDITED PART of my question this way:

ax2.set_ylim(ax1.get_ylim()) <-- This solvs the "Offset" between both scales

ax2.set_yticks(np.arange(len(ELEMENT[1])), minor=False)
ax2.set_yticklabels(ELEMENT[1])

enter image description here

Where did i find it?: How do I align gridlines for two y-axis scales using Matplotlib?

Answered by: Hugo Alain Oliva

Upvotes: 1

Sheldore
Sheldore

Reputation: 39052

To add a second y axis, you have to use:

ax2 = ax1.twinx()

where ax1 is the current/first axis. In your case ax1 is ax. Then if you plot data using ax2, the y-values will be displayed on the right hand side y axis. More info here

Upvotes: 2

Related Questions