0

When using matplotlib to graph time series data, I get strange mangled outputsenter image description here

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.

1
  • Post a portion of yourself csv file in a code block so we can test your code with your data. Commented Feb 5, 2021 at 0:26

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.