2

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.

Nils
  • 409
  • 6
  • 16

3 Answers3

2

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

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Oh I understand now what you mean. To make both the `y` axis have the same scale, you can use `ax.set_ylim(a, b)` and `ax2.set_ylim(a, b)` where `a` and `b` are the minimum and maximum `y` value you want to see on the `y` scales. Here I assume that your axes are named `ax` and `ax1`. Modify accordingly if you have other names like `ax1` and `ax2` – Sheldore Aug 21 '18 at 18:18
  • No, no, i have the same limits, but there is an offset between left an right. :-/ – Nils Aug 21 '18 at 18:24
  • I think I have to study this: https://stackoverflow.com/questions/25159495/multiple-y-axis-conversion-scales – Nils Aug 21 '18 at 18:35
  • Yeah, this is exactly what you are facing. Did you try doing `ax.set_yticks(np.arange(len(TITLES)), minor=False)` and `ax1.set_yticks(np.arange(len(TITLES)), minor=False)`. I mean exact same range for both the axes? Do you still get the same problem? – Sheldore Aug 21 '18 at 19:06
  • Yeah, they got the same range, but there is still an "offset" between the grids. :-/ – Nils Aug 21 '18 at 19:13
  • Oh try the method in the link you shared then. – Sheldore Aug 21 '18 at 19:35
  • 1
    Last answer of this link: https://stackoverflow.com/questions/26752464/how-do-i-align-gridlines-for-two-y-axis-scales-using-matplotlib/29542999 solved mit problem. :) – Nils Aug 21 '18 at 19:56
1

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

Nils
  • 409
  • 6
  • 16
0

I decided that using subplots was less confusion and put two heat maps side by side

Golden Lion
  • 3,840
  • 2
  • 26
  • 35