matplotlib:数据点在折线图中以错误的顺序连接 [英] matplotlib: data points connected in wrong order in line graph

查看:66
本文介绍了matplotlib:数据点在折线图中以错误的顺序连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取一个熊猫数据框,并试图从中生成一个图.在该图中,数据点似乎按照由y值升序确定的顺序连接,从而产生了一个奇怪的之字形曲线,如下所示:

I'm reading a pandas dataframe, and trying to generate a plot from it. In the plot, the data points seem to be getting connected in an order determined by ascending y value, resulting in a weird zig-zagging plot like this:

代码如下:

from pandas import DataFrame as df
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

data = df.from_csv(...)

plt.plot(data['COL1'], data['COL2'])

关于如何固定点连接顺序的任何建议(即,按点在图中从左到右的顺序连接它们)?谢谢.

Any suggestions on how to fix the order in which the dots are connected (i.e. connect them in the sequence in which they appear going from left to right on the plot)? Thanks.

推荐答案

COL1 中值的顺序是否与 csv 不同?

Is the order of values in COL1 different from the csv?

您可以先按COL1进行排序,然后在绘制前将其添加:

You can sort by COL1 first, add this before plotting:

data.sort('COL1', inplace=True)

这篇关于matplotlib:数据点在折线图中以错误的顺序连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆