简体   繁体   中英

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

在此处输入图像描述

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='--')

在此处输入图像描述

With sort:

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM