Reputation: 59
When using matplotlib to graph time series data, I get strange mangled outputs
This only happens when I convert the time series string in the csv to a datetime object. I need to do this conversion to make use of Matplotlib's time series x axis labelling.
abc = pd.read_csv(path + "Weekly average capacity factor and demand - regular year" + ".csv", parse_dates=True, index_col="Month", header=0)
fig, ax = plt.subplots()
x = abc.index
y1 = abc["Wind"]
curve1 = ax.plot(x, y1)
pd.read_csv(parse_dates=True)
creates the index as a datetime64[64] object. Perhaps this isn't optimized for use by matplotlib??
How can I make this work.
Upvotes: 0
Views: 109
Reputation: 1432
Your date index is not in order. Matplotlib does not sort the data prior to plotting, it assumes list order is the data order and tries to connect the points (You can check this by plotting a scatter plot and your data will look fine). You have to sort your data before trying to plot it.
abc = pd.read_csv(path + "Weekly average capacity factor and demand - regular year" + ".csv", parse_dates=True, index_col="Month", header=0)
x, y1 = zip(*sorted(zip(abc.index, abc["Wind"])))
fig, ax = plt.subplots()
curve1 = ax.plot(x, y1)
Upvotes: 1