1

I know how to plot twin y-axis but for my problem, I need to make a twin y-axis in percentages (like 0.1 = 10% to 1 = 100%) and here the maximum value or the end point of the line graph should be matched with '1' i.e.100%. For example: x = 1,3,6,8 & y=2,7,9,17 now if we plot this we know the highest point is (8,17) then w.r.t. this point(8,17) I need to show on the twin y-axis as '1' the twin y-axis can be a dummy axis as it is just needed as a reference axis to compare the graph.

I tried using normalization method but the original plot is getting disturbed and also the y-axis points are displayed according to new twin axis not with the original y-axis values.

For reference, this is the current output:

enter image description here

Below is my code which I tried.

import numpy as np
import matplotlib.pyplot as plt

#below x and y points can be changed according to the user
y = np.array([0, 38, 71, 99, 123, 205])
x = np.array([0, 0.37, 0.74, 1.11, 1.48, 1.85])

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x, y,'b--')
yrep = y-min(y[:])
yrep = yrep/max(yrep[:])
print (yrep,"yrep")
ax2 = ax1.twinx()
ax2.plot(x, yrep, alpha=0)

plt.show()
Bart
  • 9,825
  • 5
  • 47
  • 73
saira
  • 21
  • 3
  • Setting `ax1.set_ylim(y.min(), y.max())` and `ax2.set_ylim(0, 1)` should be sufficient in your specific example? – Bart Dec 06 '17 at 07:51
  • Yes this is working as I need but the only problem is when I place my cursor on the line the y-coordinate values are showing in the range 0 to 1 not as per the original y-axis values ( 0, 38, 71, 99, 123, 205). – saira Dec 06 '17 at 07:57
  • This doesn't sound like something which is very easy to fix (see e.g. this question/answer: https://stackoverflow.com/questions/16672530/cursor-tracking-using-matplotlib-and-twinx/16672970#16672970) – Bart Dec 06 '17 at 08:03
  • Thanks for the link but is it possible to make dummy twin axis beacuse I need to do other plottings w.r.t. original y-axis values. Now the problem is if I am trying to plot points on the line graph by taking y-axis values but it is taking twin y-axis values 0 to 1. – saira Dec 06 '17 at 08:58
  • I don't understand your last question/point. You can plot on the original (left) axis using e.g. `ax1.plot(..)`? – Bart Dec 06 '17 at 09:37
  • Sorry, I mean to say why when I keep the cursor on the graph it is showing y-coordinates values from the twin axis but not the original y-axis. In the link you mentioned it is showing all 3 axes and it is bit complicated as I don't need to display the all coordinates; I should only able to see the x and original y coordinates in the bottom right side of the graph. – saira Dec 06 '17 at 10:26
  • Based on several questions here on SO, it seems that there is no easy solution to fix that. – Bart Dec 06 '17 at 10:52
  • The code from the question shows the coordinates of the first axes (ax1). See [image](https://i.stack.imgur.com/vxbrA.png). So you would probably just want to update your matplotlib version. – ImportanceOfBeingErnest Dec 06 '17 at 11:49
  • What exactly you mean 'update your matplotlib version'. Do you mean I need to install new version of matplotlib. – saira Dec 06 '17 at 13:56
  • Is it possible to make the ticks and gridlines aligned on both y-axis. I'm looking for a general way to do that rather than doing manually. i.e. if I get any two datasets of yaxis and twin y-axis how do I set up the plot in a way that the gridlines coincides for two axis. – saira Dec 08 '17 at 09:36

0 Answers0