0

I am trying to plot two different lines from a same vector in python using Matplotlib. For this, I use an additional vector whose values on certain indices filter the general array to plot in a certain line. The code is:

import matplotlib.pyplot as plt 

def visualize_method(general, changes):
    '''Correctly plots the data to visualize reversals and trials'''
    x = np.array([i for i in range(len(general))])
    plt.plot(x[changes==0], general[changes==0],
             x[changes==1], general[changes==1], 
             linestyle='--', marker='o')
    plt.show()

When plotting the data, the result is: enter image description here

As it can be observed, the y axis is "duplicated", how could I use the same y and x axis for this filtered plot?

Tai
  • 7,684
  • 3
  • 29
  • 49
JsMartinez
  • 321
  • 1
  • 5
  • 16
  • I don't have matplotlib installed yet, but did you try ```plt.plot( x[changes==0], general[changes==0], 'r--', x[changes==1], general[changes==1], 'b^' )``` It can just be the case that the function signature that your using is confusing to matplotlib, hence the unexpected result? Also check out https://stackoverflow.com/questions/4805048/how-to-get-different-colored-lines-for-different-plots-in-a-single-figure it's dealing with something similar. – TuanDT Dec 25 '17 at 23:40
  • The ordering in the parameters produces the same results, as well as the solution in the posted discussion – JsMartinez Dec 26 '17 at 03:56
  • Please try to come up with a [minimal working example](https://stackoverflow.com/help/mcve), i.e. one that we can directly run. For this you have to come up with some fictitious data. – Tom de Geus Dec 26 '17 at 10:16
  • Probably youry values are not numbers but strings. That's why you see that. Cast them to numbers (same for the X axis) – Sembei Norimaki Mar 26 '23 at 13:35

1 Answers1

0

I know this is a really old thread but in case it helps anybody else, I had the same problem. In my case, the problem was that the Y values in my arrays were not defined as a numeric type. I solved my problem by wrapping those values in a float() function. In this case that would have looked like this:

 ...plt.plot(x[changes==0], [float(y) for y in general[changes==0]],...
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
user2923409
  • 25
  • 1
  • 3