Find a value from x axis that correspond to y axis in matplotlib python

2024/12/9 22:15:47

I am trying to do simple task such as to read values of x axis that corresponds to value of y axis in matplotlib and I cannot see what is wrong.

In this case I am interested for example to find which value for y axis I get if I choose x=2.0, but I get idx tuple empty even there is number 2 in xvalues array.

This is the code:

pyplot.plot(x,y,linestyle='--',linewidth=3)ax = pyplot.gca()line = ax.lines[0]xvalues = line.get_xdata()yvalues = line.get_ydata()idx = where(xvalues == 2.0) y = yvalues[idx[0][0]]

This is the xvalues array:

[1.40000000e+00   1.45000000e+00   1.50000000e+00   1.55000000e+00
1.60000000e+00   1.65000000e+00   1.70000000e+00   1.75000000e+00
1.80000000e+00   1.85000000e+00   1.90000000e+00   1.95000000e+00
2.00000000e+00   2.05000000e+00   2.10000000e+00   2.15000000e+00
2.20000000e+00   2.25000000e+00   2.30000000e+00   2.35000000e+00]
Answer

The reason you're getting an empty array is that the strict value 2.0 doesn't actually exist in your array.

For example:

In [2]: x = np.arange(1.4, 2.4, 0.05)In [3]: x
Out[3]:
array([ 1.4 ,  1.45,  1.5 ,  1.55,  1.6 ,  1.65,  1.7 ,  1.75,  1.8 ,1.85,  1.9 ,  1.95,  2.  ,  2.05,  2.1 ,  2.15,  2.2 ,  2.25,2.3 ,  2.35])In [4]: x == 2.0
Out[4]:
array([False, False, False, False, False, False, False, False, False,False, False, False, False, False, False, False, False, False,False, False], dtype=bool)In [5]: np.where(x == 2.0)
Out[5]: (array([], dtype=int64),)

This is a classic gotcha of floating point math limitations. If you'd like, you could do:

y[np.isclose(x, 2)]

However, in general, you're wanting to interpolate your y-values at a given x.

For example, let's say you wanted the value at 2.01. That value doesn't exist in your x-array.

Instead, use np.interp to do linear interpolation:

In [6]: y = np.cos(x)In [7]: np.interp(2.01, x, y)
Out[7]: -0.4251320075130563
https://en.xdnf.cn/q/72891.html

Related Q&A

Django accessing OneToOneField

Made a view that extended User:class Client(models.Model):user = models.OneToOneField(User, related_name=user)def __unicode__(self):return "%s" % (self.user) I am currently trying to access…

Pandas DataFrame: copy the contents of a column if it is empty

I have the following DataFrame with named columns and index:a a* b b* 1 5 NaN 9 NaN 2 NaN 3 3 NaN 3 4 NaN 1 NaN 4 NaN 9 NaN 7The data…

Solving the most profit algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

Get combobox value in python

Im developing an easy program and I need to get the value from a Combobox. It is easy when the Combobox is in the first created window but for example if I have two windows and the Combobox is in the s…

PyAudio (PortAudio issue) Python

I installed pyaudio with anaconda python. Using conda install pyaudio on windows. It said it installed and it also installed PortAudio with it.However, when I create my file and run it now I get the fo…

Python multiprocessing with M1 Mac

I have a mac (Mac Os 11.1, Python Ver 3.8.2) and need to work in multiprocessing, but the procedures doesn’t work. import multiprocessingdef func(index: int):print(index)manager = multiprocessing.Mana…

What is faster in Python, while or for xrange

We can do numeric iteration like:for i in xrange(10):print i,and in C-style:i = 0 while i < 10:print i,i = i + 1Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as…

Concatenate Numpy arrays with least memory

Not I have 50GB dataset saved as h5py, which is a dictionary inside. The dictionary contains keys from 0 to n, and the values are numpy ndarray(3 dimension) which have the same shape. For example:dicti…

How to generate random programs from BNF

I know my question sounds a little vague, but I could not find any tutorials online. I am not asking for an answer, but for more of an explanation. An example of the BNF:<prog> ::= “int main() {…

Pandas: merge multiple dataframes and control column names?

I would like to merge nine Pandas dataframes together into a single dataframe, doing a join on two columns, controlling the column names. Is this possible?I have nine datasets. All of them have the fo…