Home > front end >  Connect points in order of x axis for df.plot.line
Connect points in order of x axis for df.plot.line

Time:01-04

I am trying to plot some values grouped by a column and compare them. I used

              for key, grp in df.groupby([gcol]):
                     ax = grp.plot.line(ax=ax,linestyle="--",marker="o",  x='steps', y='rouge_score', label=key, color=colors[gi])
                     gi  = 1
                     if gi > len(colors) - 1: gi = 0
                     name  = key   "_"
               ax.set_xticks(df["steps"].unique())
               ax.set_title(name)


However, as you see in the picture, it connects the points in order of y axis, while I want to connect them in order of x axis. For example for the red line it must first be connected to the point at 4500 and then the point at 9000

enter image description here

CodePudding user response:

Sort your values by x:

Setup a MRE:

df = pd.DataFrame({'x': [0, 2, 1], 'y': [0, 1, 2]})

Without sort:

df.plot.line(x='x', y='y', marker='o', ls='--')

enter image description here

With sort:

df.sort_values('x').plot.line(x='x', y='y', marker='o', ls='--')

enter image description here

  •  Tags:  
  • Related