-5

I want to plot just values which are less or equal a certain number (let's say 15). I can find these values in one column of my dataframe and if these values aren't less or equal 15, I don't want to plot them.

Can you help me find a solution?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Leonardo Ferreira
  • 385
  • 1
  • 3
  • 11
  • What have you tried so far? If you can find those values, you can make a list of them and plot that list. You can also change the [limits of the axes](http://stackoverflow.com/questions/3777861/setting-y-axis-limit-in-matplotlib) to "hide" the values you don't want in this case. – berna1111 Apr 03 '17 at 19:29
  • Although being of really bad quality (@Leonardo Please read [ask]), I think this question is clear enough to be answered. – ImportanceOfBeingErnest Apr 03 '17 at 21:39

1 Answers1

7

0. Produce a minimal example
(This step should normally be taken by the one who asks the question; which is one of the reasons your question was downvoted so often)

import numpy as np; np.random.seed(4)
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams["figure.figsize"] = 5,3

x = np.linspace(2.1,6.3,num=70)
y = np.cumsum(np.random.normal(size=len(x))*10.)
df = pd.DataFrame({"x":x, "y":y})

plt.plot(df.x,df.y)

plt.show()

Now you have several options:

1. Limit the view

You can simply limit the view of the plot such that its y scale only goes up to 15. This is done via

plt.ylim(-30,15)

enter image description here

2. Filter the dataframe

You can filter the dataframe by a condition. This means that in the resulting dataframe only those rows where the condition is meat are present.

df2 = df[df.y <= 15]

plt.plot(df.x,df.y, label="original")
plt.plot(df2.x,df2.y, label="filtered to y <= 15")

enter image description here

As can be seen, the values above 15 are not in the filtered curve. However, it also looks like the new curve beeing continuous, which may be confusing depending on what graph is desired.

3. Setting all values above 15 to nan

Values in a dataset which are nan (not a number) are not plotted. This can be used, by setting all values above 15 to nan.

df2 = df.copy()
df2[df2.y > 15] = np.nan

plt.plot(df2.x,df2.y, label="y > 15 set to nan")

enter image description here

Appendix: Using two different conditions
You can use several conditions by combining them with logical operators like & ("and") or | ("or"). So setting all values which are either above 15 or below -15 to nan, you'd do

df2[(df2.y > 15) | (df2.y < -15)] = np.nan
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I want to thank you, your answer, helped me a lot. – Leonardo Ferreira Apr 04 '17 at 12:57
  • I got one more question to do. I'm using this: x = a['Roll'] x2 = x[x <= 15] plt.plot(a['Roll']) plt.plot(x2) plt.legend(['original','filtered to x2 <= 15']) plt.grid() plt.show() But I just realized that I need to plot x2 not if it's only 15 or less, I need to plot if x2 is greater or equal than -15 and if it's less or equal than 15 (that was what you explained to me), but if I want to plot my values if -15 <= x >= 15, how I do this? – Leonardo Ferreira Apr 04 '17 at 13:06
  • I updated the answer. – ImportanceOfBeingErnest Apr 04 '17 at 13:52
  • It works if I was using the third way that you taught me, but I'm using the second. I tried to applicate this to the second way but didn't work. Imagine one column of data, I want to compare plotting all data of this column with the conditional plot that is plotting only the datas in this condition >>> -15 <= x >= 15 of the same column in the same graphic. – Leonardo Ferreira Apr 04 '17 at 14:31
  • Combining conditions works with both methods! – ImportanceOfBeingErnest Apr 04 '17 at 14:53