Matplotlib plot plotting the wrong data values

Viewed 37

I am trying to plot random rows in a dataset, where the data consists of data collated across different dates. I have plotted it in such a way that the x-axis is labelled for the specific dates, and there is no interpolation between dates.

The issue I am having, is that the values plotted by matplotlib, do not match the entry values in the dataset. I am unsure as to what is happening here, would anyone be able to provide some insight, and possibly as to how I would fix it?

I have attached an image of the dataset and the plot, with the code contained below.

The code for generating the x-ticks, is as follows:

In: #creating a flat dates object such that dates are integer objects
flat_Dates_dates = flat_Dates[2:7]
flat_Dates_dates

Out: [20220620, 20220624, 20220627, 20220701, 20220708]

In: #creating datetime object(pandas, not datetime module) to only plot specific dates and remove interpolation of dates
date_obj_pd = pd.to_datetime(flat_Dates_dates, format=("%Y%m%d"))

Out: DatetimeIndex(['2022-06-20', '2022-06-24', '2022-06-27', '2022-07-01',
               '2022-07-08'],
              dtype='datetime64[ns]', freq=None)

Dataset being plotted, as well as the following code:

As you can see from the dataset, the plotted trends should not take that form, the data values are wildly different from where they should be on the graph.

Edit: Apologies, I forgot to mention x = date_obj_pd - which is why I added the code, essentially just the array of datetime objects.

y is just the name of the pandas DataFrame (data table) I have included in the image.

1 Answers

You are plotting columns instead of rows. The blue line contains elements 1:7 from the first column, namely these:

enter image description here

If you transpose the dataframe you should get the desired result:

plt.plot(x, y[1:7].transpose(), 'o--')

Related