Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Connect points in order of x axis for df.plot.line

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading