0

I want to graph a pandas data frame named vslr (414 rows, 2 columns, but the graph I am getting is unusable.

The command I am using to plot:

plt.plot(vslr['Price'],vslr['Date'])

The graph: enter image description here

My data:

print(vslr.head)
                 Date   Price
0    2020-01-31 15:30:00  8.1653
1    2020-01-31 14:30:00  8.2087
2    2020-01-31 13:30:00  8.1753
3    2020-01-31 12:30:00  8.1551
4    2020-01-31 11:30:00  8.0903
..                   ...     ...
409  2019-11-05 13:30:00  6.8452
410  2019-11-05 12:30:00  6.8050
411  2019-11-05 11:30:00  6.7600
412  2019-11-05 10:30:00  6.7553
413  2019-11-05 09:30:00  6.6502

vslr.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 414 entries, 0 to 413
Data columns (total 2 columns):
Date     414 non-null object
Price    414 non-null object
dtypes: object(2)
memory usage: 6.6+ KB

Help is appreciated :)

Rahel Miz
  • 159
  • 1
  • 9

2 Answers2

1

IIUC, you are getting a 'useless' graph becasue of the axis you have choosen to plot Date on, normally you would plot Date on x-axis and prices on y-axis and then check for your graph. In Matplotlib.plot() first argument is plotted on x-axis and secon argument is plotted on y-axis

You may need to sort your dataframe by date as well in ascending order for this use:

vslr.sort_values(by='Date', ascending=True, inplace=True)
plt.plot(vslr['Date'],vslr['Price'])

Note since this is a DateTime time column, missing dates will get their ticks as well. Your main goal seems to be plotting over prices vs date so you can extract them from your Date columns as well.

vslr['Date']=pd.to_datetime(vslr['Date'])
vslr['Date']=vslr['date'].dt.date

If you set the index to the datetime matplotlib will handle the x axis for you. Here an example

import pandas as pd
import matplotlib.pyplot as plt

date_time = ["2011-09-01", "2011-08-01", "2011-07-01", "2011-06-01", "2011-05-01"]
date_time = pd.to_datetime(date_time)
temp = [2, 4, 6, 4, 6]

DF = pd.DataFrame()
DF['temp'] = temp
DF = DF.set_index(date_time)

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.3)
plt.xticks(rotation=90)
plt.plot(DF)

Setting the df index to the datetime series allows matplotlib to deal with x-axis on time series data, also look into this link for handling spacing on x-axis.

M_S_N
  • 2,764
  • 1
  • 17
  • 38
0

As i can observe you are dealing with time series data, if you just want to plot it without some pre processing( diving it into months maybe be yearly ), follow this article here.

but let us suppose you like to divide your data set into segments you could do the following using date properties , and then plot it over that axis.

 vslr['date']=pd.to_datetime(vslr['date'])
 vslr['month']=vslr['date'].dt.month
 vslr.groupby('month').aggregate({'Price':'sum'}).reset_index()
mohsinali
  • 286
  • 1
  • 9