Matplotlib: data points connected in the wrong order in a line graph

I am reading the pandas framework and trying to generate a graph from it. On the graph, the data points are apparently connected in the order determined by the increasing value of y, which leads to a strange plot of a zigzag graph:

enter image description here

The code looks something 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 of connecting the points (i.e. connect them in the sequence in which they are displayed from left to right on the graph)? Thanks.

+4
source share
1 answer

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

First you can sort by COL1, add this before plotting:

 data.sort('COL1', inplace=True) 
+3
source

Source: https://habr.com/ru/post/1492689/


All Articles