0
$\begingroup$

I have two numpy arrays, one with the data for x axis and another one with the data for the y axis. The position i in each array matches the other array.

Some values in the x axis array are repeated. I would like each of them to appear in a separate area, as in the following examples (note that 5 and 10 appear twice in the x axis):

chart with just the dots chart with lines connecting the dots

Here is the code I tried:

dataFrame = pd.DataFrame({'xx': x_data, 'yy': y_data})
plt.plot('xx', 'yy', data=dataFrame, marker='o')

But this code is creating a chart where there is more than one y axis value for each x axis value (possible due to the repetitions).

The dataFrame is like this:

     xx   yy
     5    1
     10   2
     15   1
     0    0
     5    3
     20   4
      4   3
     10   4

Thanks in advance.

$\endgroup$
2
  • $\begingroup$ Could you share your arrays? and also are you looking for a scatter plot and a line plot connecting those data-points from the scattered plot? $\endgroup$
    – Aymuos
    Aug 27 '20 at 23:46
  • $\begingroup$ I added an example of the values that are in the dataFrame. I'm not sure about the technical names of the plots, but I think that is it. Thanks! $\endgroup$
    – MyName
    Aug 27 '20 at 23:56
1
$\begingroup$

Changed the values for better representation

# Gathering Dataframe
import pandas as pd 
data = [[1, 5], [1, 6], [2, 5], [0,0], [5,8], [20,4]]  
df = pd.DataFrame(data, columns = ['xx', 'yy']) 
df

#Drawing the plot (Scatter Plot)
from matplotlib.pyplot import *

plott = df.plot.scatter(x='xx', y='yy', title= "Scatter plot between two variables X 
and Y");

plott

enter image description here

# (line plot connecting the scatter plots)
plt.plot( 'xx', 'yy', data=df, linestyle='-', marker='o')
plt.show()

enter image description here

$\endgroup$
2
  • $\begingroup$ Would it be possible to have both x=1 appear in different lines along the x axis, so that their their y values (5 and 6, respectively) are not "stacked"? $\endgroup$
    – MyName
    Aug 28 '20 at 9:40
  • $\begingroup$ That would be possible by hard-coding the graph but i really don't think that'll make things fruitful. If you do that it actually changes the graph. So suppose your graph is vertically flat, it'll become horizontally flat which is completely wrong! $\endgroup$
    – Aymuos
    Aug 28 '20 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.