1

I'm facing some problems in the alignment of the ticks of two different y-axes with the first characterized by a linear range and the second by a non linear range as depicted in the following picture.

HS, TMN = np.meshgrid(hs, period)
r = function(HS, TMN)
cax = plt.contourf(HS, TMN, np.log10(HS), cmap=plt.cm.RdYlGn_r)
ax = plt.gca()
ax2 = ax.twinx()
ticks2 = get_y2values(ax.get_yticks()) # Non linear function
ax2.yaxis.set_major_locator(mpl.ticker.FixedLocator(ticks))
ax2.set_ylim([0, 700])
ax.grid()
ax.set_ylabel('Y1', fontsize=14)
ax2.set_ylabel('Y2', fontsize=14)
plt.show()

Example of the result obtained

More precisely, the right axis requires a different scale from the one on the left. And as final outcome, the idea is to have ticks values on the left aligned with the ticks values on the right (due to the non-linear function depicted below). E.g.: the value 8.08 from Y1 aligned with 101.5; 16.07 aligned with 309.5...

The new scale is required in order to insert new plot in the new scale.

Function relating y1 with y2

TommasoF
  • 795
  • 5
  • 21
  • 1
    Is this about the ticklabels or about the scale? Does the right axis need to have a different scale than the left? It would be useful if you spent more than one sentence on the actual problem. – ImportanceOfBeingErnest Apr 17 '18 at 09:40
  • Sorry, but I did not get your point. Given a different range of variability, the scale should be different between Y1 and Y2, isn't it? The values obtained on the right axis is a non linear function of the values of the left axis. So, I think it is about the scale. – TommasoF Apr 17 '18 at 09:56
  • Does the right axis need to be properly scaled or is this just about placing a ticklabel at some position? – ImportanceOfBeingErnest Apr 17 '18 at 10:00
  • Yes the right axis needs to be properly scaled and then as desired result having the ticklabels aligned. Sorry for missing this in the question. – TommasoF Apr 17 '18 at 10:03
  • [Here](https://stackoverflow.com/questions/43463431/custom-logarithmic-axis-scaling-in-matplotlib) or [here](https://stackoverflow.com/questions/45306099/how-to-put-different-in-magnitude-values-at-same-distance-on-x-axis) is how you create a custom scale. This will not align your ticks. For that you would in addition need a custom locator (in this case probably a `FixedLocator`). This seems a bit overkill, so maybe you don't need a proper scale at all? Still not too clear from the question. – ImportanceOfBeingErnest Apr 17 '18 at 10:14
  • Is the solution in the case of ticklabels the one at the following [link](https://stackoverflow.com/questions/20243683/matplotlib-align-twinx-tick-marks?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa)? – TommasoF Apr 17 '18 at 12:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169169/discussion-between-tommasof-and-importanceofbeingernest). – TommasoF Apr 17 '18 at 12:29

1 Answers1

1

As suggested in the comments the definition of a new scale works perfectly. Referring to the SegmentedScale defined at the following link, the code that worked for me is the following:

hs = np.linspace(0.1, 15, 1000) # [meters]
period = np.linspace(0.1, 35, 1000) # [seconds]

HS, TMN = np.meshgrid(hs, period)
cax = plt.contourf(HS, TMN, np.log10(HS), cmap=plt.cm.RdYlGn_r)
ax1 = plt.gca()
ax2 = ax.twinx()
ticks = get_y2values(ax1.get_yticks()) # Non linear function
ax2.set_yscale('segmented', points=ticks)

ax1.grid()
ax1.set_yticks(ax1.get_yticks())
ax2.set_yticks(ticks)
ax1.set_ylabel('Y1', fontsize=14)
ax2.set_ylabel('Y2', fontsize=14)
plt.show()

enter image description here

If it is necessary to add new plots on the ax2 axis, it is required to do the plot before the application of the new custom scale.

TommasoF
  • 795
  • 5
  • 21